Working with PHP Sessions

A pure html-based website will not be able to pass data between pages – all information associated with the current page is forgotten when a new page is loaded. This poses a problem for websites with shopping cart features, requiring that user data be retained (e.g., selected a product of the user) from one page to another.card

How PHP sessions work

The use of PHP sessions solves this problem by allowing the server to store user information for later use (i.e., username, password, selected shopping cart items, etc). PHP sessions create a unique identification number (UID) for each website visitor and then store variables based on these UIDs. This prevents user data from getting confused with each other when multiple users visit the same webpage.

Note: PHP sessions only save information temporarily – they get deleted once the user leaves the website. It is important to determine whether the temporary storage that a PHP session can provide is what you need for your website. If you need more permanent storage you will need a MySQL database instead of just PHP sessions.

How to start a PHP session

Before you can start storing user data in a PHP session, you must first start it. A session should appear at the very beginning of the code- before any regular text or HTML is read by the server.

Below is a sample script used to start a PHP session:

<?php
 session_start();
 ?>

The code snippet above will register the user session onto the server, start storing the user information, and assign a UID for the said session.

How to store a PHP session variable

Use the $_SESSION associative array to store a user data in a particular session. This is where session data are stored and retrieved. Below is the code for storing a session variable:

<?php
 session_start();
 $_SESSION[‘views’] = 1;                            //this line stores session data
 echo “Pageviews = “. $_SESSION[‘views’];           //this line retrieves session data

The above code will display the following results:

Pageviews = 1

How to use PHP isset function

The purpose of creating and storing a PHP variable into a session is for you to be able to retrieve and use it in the future. It is therefore necessary to have a function that would check whether a session variable exists or not – the isset function.

Isset is a PHP function that checks whether a variable that you want to use has already been “set” or assigned a value.

Here is a pageview counter code that checks whether the pageview variable has already been set. If it has, we increment the counter; otherwise we create a pageview counter and then set it to one.

<?php
 session_start();
 if(isset($_SESSION[‘views’])
     $_SESSION[‘views’] = $_SESSION[‘views’] + 1;
 else
     $_SESSION[‘views’] = 1;
echo “views = ‘.$_SESSION[‘views’];
 ?>

How to clear and destroy a session

Although session data are temporary and need not be cleaned afterwards, there may be some instances when various tasks require you to explicitly delete them. Say for instance you want to delete the items on the shopping cart of a client after they’ve placed an order.

<?php
 session_start();
 if (isset($_SESSION[‘cart’]))
     unset($_SESSION[‘cart’]);
 ?>

You may also destroy the session by using the session_destroy function:

<?php
 session_start();
 session_destroy();
 ?>

The session_destroy function will reset the entire session and will delete all stored data.

Image by Flickr

Article By: Wasim Ismail

Wasim’s a project manager at Alrayes Web Solutions along with an online SEO consultant & blogger for business at wasimismail.com, specialising in online business.


Comments are closed.

Three Measures To Keep Track Of A Cell Mobile Phone appforspy.com mobile tracker app