Login |  Register 
Your open source Rocks PHP Library has really made developing PHP easier!
Alan
 

Sessions

If you want to store information temporarily between pages and refreshes then sessions is a good way to do it. This stores data for a particular user at the server. In order to use sessions you must first call the start_session function.
start_session();
You can then assign values to the $_SESSION global array like so.
$username = "tom";
$_SESSION["username"] = $username;
The name username is the index of the session value and can be retrieved like a normal associative array value, so you can access the values like so...
echo $_SESSION["username"];
You can delete a session value with the unset function...
unset($_SESSION["username"]);
While session_destroy destroys all data registered to a session.
session_destroy();

How to find out if you have started a session

The function session_id will return the string id of the current session, or if there is no session it will return an empty string, so the following can detect if you currently have a session active.
if (empty(session_id())
{
 echo "You have no session";
}
If you start a session twice you can get a notice saying that a session has already started, so to get around this you can do something like...
if (empty(session_id())
{
 start_session();
}

Comments to date: 1. Page 1 of 1. Average Rating:
Ajaykumar   5:39pm on Saturday, September 4th, 2010 
Give small projects for beginners thanks keep going.

security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38