Creating a PHP Session |
PHP sessions are created using the session_start() function which should the first function call of the PHP script on your web page (i.e before any output is written to the output stream). |
The following example demonstrates the creation of a PHP session: |
<?php session_start(); $msg="Session Created"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> PHP Tutorial: Working With Session </TITLE> <META NAME="Generator" CONTENT="jNote-iT"> </HEAD> <BODY onload="alert('<?php echo $msg;?>');"> <B><?php echo $msg;?></B> </BODY> </HTML> |
![]() |
Creating and Reading PHP Session Variables |
Variables can be assigned to a session using the $_SESSION array. This is a global array that is accessible to all the pages on your web site. This is also an associative array and as such it is possible to access array elements using the variable name as an index. |
Session variables can be any type of data such as strings, numbers, arrays and objects. |
Session variables can be defined using a number of mechanisms. Variables can be assigned directly to the $_SESSION array using the assignment operating and variable name: |
<?php $_SESSION[‘userName’] = ‘XYZ’; ?> |
Another option is to use the PHP session_register() function. session_register() takes two arguments, the string representing the variable name, and the value to be assigned to the variable: |
<?php session_register(‘username’, ‘Neeraj’); ?> |
Session variables are accessed by using the variable name as an index key into the $_SESSION array. The session_is_registered() function can also be used to make sure the variable exists before attempting to read the value (which is generally considered to be good practice). |
For example: |
<?php session_start(); ?> <html> <head> <title>Simple HTML Form</title> </head> <body> <?php if (session_is_registered('userName') { $_SESSION['userName'] = 'Neeraj'; echo 'userName = ' . $_SESSION['userName']; } ?> </body> </html> |
The resulting output from the above page will read: |
userName = Neeraj |
The same PHP code to read the value can be used on any page on your web server to access the current value of the variable. |
Storing a Session Variable |
When you want to store user data in a session, use the $_SESSION associative array. This is where you both store and retrieve session data. In previous versions of PHP there were other ways to perform this store operation, but it has been updated and this is the correct way to do it. |
<?php session_start(); $_SESSION['num_page_view'] = 1; // store session data ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>PHP Tutorial :Working with Session</title> </head> <body> <?php echo "Page Views = ". $_SESSION['num_page_view']; //retrieve data ?> </body> </html> |
Display: |
![]() |
In this example we learned how to store a variable to the session associative array $_SESSION and also how to retrieve data from that same array. |
PHP Sessions: Using PHP’s isset Function |
Now that you know can easily store and retrieve data from the $_SESSION array, we can now explore some of the real functionality of sessions. When you create a variable and store it in a session, you probably want to use it in the future. However, before you use a session variable it is necessary that you check to see if it exists already! |
This is where PHP’s isset function comes in handy. isset is a function that takes any variable you want to use and checks to see if it has been set. That is, it has already been assigned a value. |
With our previous example, we can create a very simple num_page_view counter by using isset to check if the num_page_view variable has already been created. If it has we can increment our counter. If it doesn’t exist we can create a num_page_view counter and set it to one. Here is the code to get this job done: |
<?php session_start(); if(isset($_SESSION['num_page_view'])) $_SESSION['num_page_view'] = $_SESSION['num_page_view']+ 1; else $_SESSION['num_page_view'] = 1; ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>PHP Tutorial :Working with Session</title> </head> <body> <?php echo "Page Views = ". $_SESSION['num_page_view']; //retrieve data ?> </body> </html> |
The first time you run this script on a freshly opened browser the if statement will fail because no session variable num_page_view would have been stored yet. However, if you were to refresh the page the if statement would be true and the counter would increment by one. Each time you reran this script you would see an increase in view by one. |
![]() |
Cleaning and Destroying your Session |
Although a session’s data is temporary and does not require that you explicitly clean after yourself, you may wish to delete some data for your various tasks. |
Imagine that you were running an online business and a user used your website to buy your goods. The user has just completed a transaction on your website and you now want to remove everything from their shopping shopping_cart. |
<?php session_start(); if(isset($_SESSION['shopping_cart'])) unset($_SESSION['shopping_cart']); ?> |
You can also completely destroy the session completely by calling the session_destroy function. |
PHP Code: |
<?php session_start(); session_destroy(); ?> |
Destroy will reset your session, so don’t call that function unless you are completely happy losing all your stored session data! |