Tuesday 31 July 2012

How to Use a PHP Session ,assign form values to session variable


A session is a way to store information (in the form of variables) to be used across multiple pages. Unlike a cookie, specific variable information is not stored on the users computer. It is also unlike other variables in the sense that we are not passing them individually to each new page, but instead retrieving them from the session we open at beginning of each page.

Call this code mypage.php

 <?php
 // this starts the session
 session_start();

 // this sets variables in the session
 $_SESSION['color']='red';
 $_SESSION['size']='small';
 $_SESSION['shape']='round';
 print "Done";
 ?>

The first thing we do with this code, is open the session using session_start(). We then set our first session variables (color, size and shape) to be red, small and round respectively.

Just like with our cookies, the session_start() code must be in the header and 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.

So how will it know it's me? Most sessions set a cookie on your computer to uses as a key... it will look something like this: 350401be75bbb0fafd3d912a1a1d5e54. 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.

Now we are going to make a second page. We again will start with session_start() (we need this on every page) - and we will access the session information we set on our first page. Notice we aren't passing any variables, they are all stored in the session.

Call this code mypage2.php

 <?php
 // this starts the session
 session_start();

 // 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'];
 ?>

All of the values are stored in the $_SESSION array, which we access here. Another way to show this is to simply run this code:

 <?php
 session_start();
 Print_r ($_SESSION);
 ?>

You can also store an array within the session array. Let's go back to our mypage.php file and edit it slightly to do this:

 <?php
 session_start();

 // makes an array
 $colors=array('red', 'yellow', 'blue');
 // adds it to our session
 $_SESSION['color']=$colors;
 $_SESSION['size']='small';
 $_SESSION['shape']='round';
 print "Done";
 ?>

Now let's run this on mypage2.php to show our new information:

 <?php
 session_start();
 Print_r ($_SESSION);
 echo "<p>";

 //echo a single entry from the array
 echo $_SESSION['color'][2];
 ?>

 <?php
 // you have to open the session to be able to modify or remove it
 session_start();

 // to change a variable, just overwrite it
 $_SESSION['size']='large';

 //you can remove a single variable in the session
 unset($_SESSION['shape']);

 // or this would remove all the variables in the session, but not the session itself
 session_unset();

 // this would destroy the session variables
 session_destroy();
 ?>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.