Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 9 PHP Cookies and Session Introduction to JavaScript.

Similar presentations

Presentation on theme: "Week 9 PHP Cookies and Session Introduction to JavaScript."— Presentation transcript:

1 Week 9 PHP Cookies and Session Introduction to JavaScript

2 PHP COOKIES What is a cookie: it is a file that a web server saves on a computer hard disk when a user visits a website that uses cookies. It is used to identify a user. example of its use is for remembering login details, items in shopping chart and so on. When there are different elements embedded on the web page from different domains, each domain can issue its own cookie. This is then referred to as third party cookie. The setcookie() function is used to create a cookie. It must come before the html tag.

3 Syntax The syntax of the set cookie function is thus setcookie (name, value, expire, path, domain, secure, httponly). The name and value parameter are required. All other parameters are optional. Name : the server uses the name parameter to access the cookie. Value: the value of the cookie. Expire: sets the expiry date of the cookie. If not set, the cookie expires when the browser closes. It is written thus, time() + number of seconds. Path: this is indicated by a forward slash, or a subdirectory. If it is a forward slash, the cookie is available over the entire domain. It is a subdirectory, it is available only in that subdirectory Domain: the internet domain of the cookie. Secure: it has the default value of false. If the value is true, the cookie can be transferred only across a secured connection. Httponly: the default is false. If the value is true, the cookie must use only the http protocol.

4 Creating a cookie that expires in 30 days <?php $name = 'chcsl'; $value = 'school of programming'; setcookie ($name, $value, time()+2592000, "/"); ?> cookies <?php echo $_COOKIE["$name"]; ?>

5 Deleting a cookie This is done by issuing the cookie again but with a date in the past. All parameters in the function apart from the expire parameter should be same with the parameters when it was first issued.

6 SESSION it is used to store information that is used across multiple pages. one difference between session and cookie is that for a session, the information is not stored on the users’ computer. Session variables by default, stores user information to be used across multiple pages and last until the browser is closed. To start a session, the session_start() function is used. It must come before any html tag like this session_start();.

7 The session starts a user key on the computer and when a session is opened on another page, it scans the computer for a user key. If there is a match, it access that session and if not, it starts a new session. To remove all session, the session_unset() function is used and to destroy all session, the session_destroy() function is used. The session_unset() and session_destroy() function can be used in the body part of the html. only the session start should come before any html.

8 Introduction to JavaScript JavaScript is the default scripting language in html. To apply JavaScript to a web document, the Script tag is used. In html 5, the type attribute is not required. It is written thus.. It uses the extension.js It can be written in an external sheet and saved with the.js extension then referenced using the src attribute in the script tag thus:

9 JavaScript has no inbuilt print function, there are different ways of displaying JavaScript Using document. write() Using alert() Using innerHTML() Using console. log()

10 First JavaScript Program using document.write () Welcome document.write("Hello World") document. write is has same function as echo or print in php.

11 Using alert () JavaScript var name = prompt("Enter your name", ""); if (name ==“mildred") { alert("Hello Instructor!!"); } else { alert ("hello student"); }

12 Using innerHTML () JavaScript document.getElementById("fox").innerHTML ="the fox jumps over the lazy dog"; The statement tells the browser to write “the fox jumps over the lazy dog” inside an html element with an id of “fox”.

13 Literals and variables Literals are fixed values in JavaScript. A variable is defined using the var keyword. Variables are used to store data. A variable can be declared first then a value assigned to it or a value can be added directly to it when it is declared. var x ; x = 1; Is same as var x = 1; Variables should be declared at the beginning of the script. Numbers can be written with or without decimal points and text (strings) can be written with either single or double quotes. Identifiers are used to name variables and they are case sensitive. Identifier names cannot start with a number and a reserved word cannot be used as a name.

14 comments Comments in JavaScript like any other language, is used to leave notes or explain a code. It is ignored by the browser. A single line comment is written with two forward slash like this // And a multiline comment is written thus /* your text goes here More text */

15 JavaScript operators Arithmetic Operators: + addition - subtraction * multiplication /division %modulo ++ increment -- decrement Assignment Operators: = assign value to variables += same as x = x+y -= same as x = x-y *= same as x= x*y /= same as x=x/y %= same as x =x%y. it returns the remainder of a division

16 Concatenation Operator: the plus sign + is used to join strings together. Using the plus sign on two numbers will produce the sum. Adding two strings together or adding a number and a string will produce a string. var name = “james”; var age = “15”; name + age = james15;

17 Comparison Operators == equal to === value and type are equal != not equal !== value or type are not equal > Greater than < less than >= greater than or equal to <= less than or equal to.

18 Operator precedence Order of precedence in JavaScript from highest to lowest () -> expression in parenthesis are executed first ++ -- increment and decrement operators are executed before multiplication, division, modulo, addition and subtraction. */% multiplication, division and modulo have equal precedence and are executed from left to right. They have higher precedence than addition and subtraction. + - addition and subtraction have the lowest precedence.

19 JavaScript DATA TYPES String: text quoted with either single or double quotes Number: can be written with or without decimal point Boolean : true or false Array: a variable with more than one value Object: are name value pairs. It is written thus var name ={firstName =“mary”, lastName = “jacob”, age = 20}; JavaScript has dynamic types, which means same variable can be used as different types. For example var x; x = 1; x = “fox”;

20 The typeof operator is used to return the type of a variable or expression. Setting a variable to undefined empties the variable. Data type of null is an object.

21 Functions in JavaScript Function is used to separate a section of code that perform a particular task. It is declared with the keyword function followed by a name for the function and a pair of parenthesis that can take arguments. Functions can also be stored in variables without a name and called with the variable name. JavaScript functions can be called before they are declared. Function definitions: do not specify data types for parameters, do not perform type checking on passed arguments and do not check the number of arguments received.

22 Example Functions Welcome function sum(a, b){ var sum = a + b; return sum; } var x = 5; var y = 8; var answer = sum(x, y); document.getElementById(“add").innerHTML = "the sum of x and y is" +answer

23 Converting Celsius to Fahrenheit using Javascript Functions function toCelsius(fahrenheit) { return (5/9) * (fahrenheit-32); } document.write("100 F to oC is: "+ toCelsius(80));

24 Anonymous function Hello World var product = function (x, y){ return x*y ; } document.write(product (2,3));

25 Changing a text when a button is clicked javascript the story of the fox the dog's reaction Read function changetext(){ document.getElementById("fox").innerHTML = "the fox jumps over the lazy dog"; document.getElementById("dog").innerHTML = "the dog is still sleeping"; }

Download ppt "Week 9 PHP Cookies and Session Introduction to JavaScript."

Similar presentations

Ads by Google