Sessions

Sessions
Difficulty: Easy
Description: Storing information into sessions.
Author: M@ster; Date Added: 2008-12-30; Views: 7590
If you have ever been to a site that has an RPG, forum or anything that requires you to login. Well most of the time when you log in it stores your username, id, and can later take that and retrieve your information from a database.

To start with you want to put this code at the top of every page where you want to be able to retrieve the session variable.

session_start();


After you put that in your pages you can now start storing your data in sessions. Now use this code to store information into a variable:

$_SESSION['id']=1;


If you want to display that variable out on another page just use this:

//retrieve session data
echo "Your id is=". $_SESSION['id'];


Your output for this would be:

Your id is=1

If you want to get rid of the $_SESSION['id'] variable you can use this:

unset($_SESSION['id']);


This will make $_SESSION['id'] equal nothing. You can also use session_destroy(); to do the same thing but it deletes are your Session variables.

Here is the full example:
session_start();

//stores the id in the session
$_SESSION['id']=1;

//retrieve session data
echo "Your id is=". $_SESSION['id'];


Hope this tutorial helped.

-Ryan