Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sessions Brendan Knight A visitor accessing your web site is assigned a unique id. This id links to specific data that remains on the server. Sessions.

Similar presentations


Presentation on theme: "Sessions Brendan Knight A visitor accessing your web site is assigned a unique id. This id links to specific data that remains on the server. Sessions."— Presentation transcript:

1 Sessions Brendan Knight A visitor accessing your web site is assigned a unique id. This id links to specific data that remains on the server. Sessions are a way of making data available to multiple web pages in a site.

2 Sessions  The session id is a token granted to the user at the beginning of a session by the server  Looks like: PHPSSID = 350401be75bbb0fafd3d912a1a1d5e54  The client usually stores and sends the id as a parameter in GET or POST queries.as a parameter in GET or POST queries.

3 session_start() Is a php method It creates a new session and id, or it resumes one based on a current session identifier. Must be used It initializes the variables

4 Start_a_session.php <?php // this starts the session session_start(); // this sets variables in the session $_SESSION['color']=‘”red”; $_SESSION['size']=“small”; $_SESSION['shape']=“round”; $_SESSION[‘fname’]= $_POST[‘fname’]; $_SESSION[‘ANYOTHERSESSIONVARIABLEYOUWANT’] = “”; //You can store arrays and all sorts of things if you want. } ?>

5 carefull the session_start() code  must be in the headermust be in the header  you can not send anything to the browser before it. you can not send anything to the browser before it.  It's best to just put it directly after the <?php to avoid potential problems. It's best to just put it directly after the <?php to avoid potential problems.

6 We move to Page2.php Then when a session is opened on another page, it scans your computer for a key. If there is a match, it accesses that session, if not it starts a new session for you. The variables are available for use on the new page

7 Page2.php code New page new start code <?php // this starts the session session_start(); //we have to do this on every new page // echo variable from the session, we set this on our other page echo "Our color value is ".$_SESSION['color']; echo "Our size value is ".$_SESSION['size']; echo "Our shape value is ".$_SESSION['shape']; ?>

8 What do you read on the page? <?php // this starts the session session_start(); //we have to do this on every new page // echo variable from the session, we set this on our other page echo "Our color value is ".$_SESSION['color']; echo "Our size value is ".$_SESSION['size']; echo "Our shape value is ".$_SESSION['shape']; ?>

9 Page2.php All of the values are stored in the $_SESSION array, which we can access here. Another way to show this is to simply run this code: <?php session_start(); Print_r ($_SESSION); ?>

10 How does it end When we have finished with a session we destroy it The function session_destroy() deletes the file. If a session terminates without going through session_destroy(), it times out and self­ destructs after some specified interval.

11 Destroy.php <?php // this starts the session session_start(); //we have to do this on every new page session_destroy(); // it destroys the id and everything ?>

12

13 Refer http://www.9lessons.info/2009/09/php-login-page-example.html http://www.knowledgesutra.com/forums/topic/7887-php-simple-login- tutorial/


Download ppt "Sessions Brendan Knight A visitor accessing your web site is assigned a unique id. This id links to specific data that remains on the server. Sessions."

Similar presentations


Ads by Google