3"> 3">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)

Similar presentations


Presentation on theme: "SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)"— Presentation transcript:

1 SESSIONS 27/2/12 Lecture 8

2 ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression) ? Returned_if_expression_is_true: returned_if_expression_is_false; 2

3 Example Example <?php $mood="sad"; $text=($mood=="happy")?"I’m in a good mood":"Not happy but $mood"; print "$text"; ?> 3

4 Date The PHP date() function is used to format a time or a date The PHP date() function formats a timestamp to a more readable date and time. 4

5 Example Untitled 1 <?php echo("Result with date(): "); //The ISO-8601 numeric representation of a day (1 for Monday through 7 for Sunday) echo(date("l"). " "); //d - date in numbers //S - suffix for date //F - A full textual representation of a month (January through December) //Y - A four digit representation of a year //h - 12-hour format of an hour (01 to 12) //i - Minutes with leading zeros (00 to 59) //s - Seconds, with leading zeros (00 to 59) //A - Uppercase AM or PM echo(date("l dS \of F Y h:i:s A"). " "); ?> 5

6 Server Side Includes You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function The two functions are identical in every way, except how they handle errors The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error) 6

7 Include Files These two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages This can save the developer a considerable amount of time This means that you can create a standard header or menu file that you want all your web pages to include 7

8 include() The include() function takes all the text in a specified file and copies it into the file that uses the include function. 8

9 <?php include("inc2.php"); ?> Welcome to my home page Some text 9

10 Include File – incnew.php Yahoo | Hotmail | ireland 10

11 The require() Function Identical to include(), they only handle errors differently The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error) 11

12 Sessions When you are working with an application, you open it, do some changes and then you close it The computer knows who you are. It knows when you start the application and when you end But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state

13 Sessions A PHP session allows you to store user information on the server for later use (i.e. username, shopping items, etc) Session information is temporary and will be deleted after the user has left the website If you need a permanent storage you may want to store the data in a database A PHP session variable is used to store information about, or change settings for a user session Session variables hold information about one single user, and are available to all pages in one application

14 Session Variables Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID The UID is either stored in a cookie or is propagated in the URL

15 Starting a session This code register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.

16 Storing a session variable <?php session_start(); // store session data $_SESSION['newuser']=1; ?> <?php //retrieve session data echo "New user has viewed the page=". $_SESSION['newuser']; ?>

17 Session Example <?php session_start(); if(isset($_SESSION['pageview' ])) $_SESSION['pageview']=$_ SESSION['pageview']+1; else $_SESSION['pageview']=1; echo "Number of times page viewed by user=". $_SESSION['pageview']; ?>

18 Killing a Session If you wish to delete some session data use the unset() or the session_destroy() function The unset() function is used to free the specified session variable session_destroy() will reset your session and you will lose all your stored session data

19 End a Session <?php session_start(); if(isset($_SESSION['pageview'])) $_SESSION['pageview']=$_SE SSION['pageview']+1; else $_SESSION['pageview']=1; echo "Number of times page viewed by user=". $_SESSION['pageview']; unset($_SESSION['pageview']); ?> <?php session_destroy(); ?>

20 More Sessions A PHP session is started either explicitly by session_start(), or implicitly by registering a variable for the session, using session_register() Call session_start() on top of the page, so that session variables are available to your script, and register variables to the session later in the script

21 More Sessions if you registered your session variables with session_register() in the head of the script and left out the session_start() call session_register() calls session_start() internally, if the session isn't started yet When you start a session, the following happens:  PHP checks whether a valid session ID exists  If there is no session ID, PHP creates a new ID  If a valid ID exists, the frozen variables of that session are reactivated and introduced back to the global namespace

22 Sessions Note that this function takes the name of a variable as argument, not the variable itself You can use session_unregister() to remove variables from the session, for example, when the user removes a product item from the shopping cart

23 <?php session_start(); print(" "); $_SESSION["MyLogin"] = "Ciara"; print("A value saved in the session named as Login.\n"); $_SESSION["MyColor"] = "Blue"; print("A value saved in the session named as Colour.\n"); print("Click Next Page "." to retrieve the values.\n"); print(" \n"); ?> New Page 1

24 <?php session_start(); print(" "); $myLogin = $_SESSION["MyLogin"]; print("Value of MyLogin has been retrieved: ".$myLogin."\n"); $myColor = $_SESSION["MyColor"]; print("Value of MyColor has been retrieved: ".$myColor."\n"); print(" \n"); ?> New Page 1

25 PHP Cookies Cookie is often used to identify a user A cookie is a small file that the server embeds on the user's computer Each time the same computer requests a page with a browser, it will send the cookie too With PHP, you can both create and retrieve cookie values 25

26 How to create a cookie? The setcookie() function is used to set a cookie The setcookie() function must appear BEFORE the tag Syntax  setcookie(name, value, expire, path, domain); 26

27 setcookie(name, value, expire, path, domain); Name = $_COOKIE[“cookiename”] Value = value of cookie Expire= expiry time of cookie Path=the path on the server on which the cookie will be available e.g. test/exam/ Domain = the domain on which it will be available e.g. example.com Secure= whether or not it should use a secure HTTP connection 27

28 How to retrieve a Cookie value? The PHP $_COOKIE variable is used to retrieve a cookie value Use the isset() function to find out if a cookie has been 28

29 Cookie Example <?php setcookie("user","Nora Batty",time()+3600); setcookie("age", 6,time()+3600); setcookie("country","Ireland",time()+3600); ?> Set Cookie and Read Cookie on same page <?php echo $_COOKIE["user"]; echo " "; echo $_COOKIE["age"]; echo " "; echo $_COOKIE["country"]; ?> 29

30 How to delete a cookie? When deleting a cookie you should assure that the expiration date is in the past Example <?php // set the expiration date to one hour ago setcookie("user", "", time()-3600); ?> 30

31 If your browser does not support cookies? If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application One method is to pass the data through forms 31

32 Cookie Example <?php setcookie("veg","carrot", time()+3600); ?> New Cookie <?php if (isset($_COOKIE["veg"])) print $_COOKIE["veg"]; else print " Hello. This may be your first visit "; ?> 32

33 Cookies In order to see all of the cookies set: print_r($_COOKIE); This will show all cookies currently contained in the cookie array 33

34 More Cookies <?php setcookie("LoginName","Ciara"); setcookie("PreferredColor","Blue"); print("2 cookies were delivered.\n"); ?> New Page 1 Move to get cookie page

35 Cookie <?php if (isset($_COOKIE["LoginName"])) { $loginName = $_COOKIE["LoginName"]; print("Received a cookie named as LoginName: ".$loginName."\n "); } else { print("Did not received any cookie named as LoginName.\n "); } print("All cookies received:\n "); foreach ($_COOKIE as $name => $value) { print " $name = $value\n "; } ?>

36 <?php $Month = 2592000 + time(); //this adds 30 days to the current time setcookie("AboutVisit", date("F jS - g:i a"), $Month); ?> Cookie Tracking <?php if(isset($_COOKIE["AboutVisit"])) { $last = $_COOKIE["AboutVisit"]; echo "Welcome back! You last visited on ". $last; } else { echo "Welcome to our site!"; } ?>


Download ppt "SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)"

Similar presentations


Ads by Google