Wednesday, October 6, 2010

Sessions in PHP

Hi to all!! I am back with a new concept in PHP Sessions.

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.


A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions.

It is important to ponder if the sessions' temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database.

Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage.

Working with Sessions:
Before you can store user information in your PHP session, you must first start up the session.

This tiny piece of code will register the user's session with the server, allow you to start saving user information and assign a UID (unique identification number) for that user's session.

The code above will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session.

Storing a Session Variable

The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:












Destroying a Session




unset($_SESSION['views']);
?>

other way to destroy the session variables:


session_destroy();
?>

No comments:

Post a Comment