
So what are sessions anyway?
Sessions in PHP in their basic form are a way of storing information about the current user (session) that is kept from page to page. Each user is given a session id when they enter the site and this is kept with them for their duration of the visit.
What do i need that for?
Sessions are often used to remember if the current user has logged into a site using a username and password. it may also be used to enter other information.
How to initiate a session
On every page of a PHP script that you need to save information in a session or gain access to previously saved information of the current session you must start the session. it is important that this is started before any other code!
<?php
//Start/Resume the session
session_start();
?>
as you can see if you upload and run this file their is no output, however if you check your cookies ....
type
javascript:alert(document.cookie)
in your address bar and press enter) you will see that your browser has obtained a session ID from PHP at the server.
The Session Global Variable
All information in a session is stored and read from an array variable called $_SESSION
to store something in the variable you would do something like this:
<?php
//Start/Resume the session
session_start();
//Store "Goughy000" and "Jack Gough" in the $_SESSION array
$_SESSION['username'] = "Goughy000";
$_SESSION['real_name'] = "Jack Gough";
//Give some output to the browser...
echo "Saved!";
?>
upload the file and run it if you wish.
the string "Goughy000" is now stored in $_SESSION['username'] and the string "Jack Gough" is now stored in $_SESSION['real_name'];
the next thing you would want to do is read from the session variable
as before we need to start the session as usual, we can then use the $_SESSION variable just like any other variable. however it will already have $_SESSION['username'] and $_SESSION['real_name'] set if you run the other script first.
so start a new script and try the following code...
<?php
//Start/Resume the session
session_start();
//echo out the data we have
echo $_SESSION['username'] . "'s Real name is... " . $_SESSION['real_name'];
?>
upload the new script with the script from before but keep them seperate, (ie, dont overwrite the first one with this one)
now.. if you run the first script then run the second script you will see that the second script has the data set in the first script. this data will be kept in the $_SESSION array untill a time limit is reached or it is removed.
I hope that bought in an easy intro to sessions, i'll do something more advanced soon
Removing session data
You can either wait for the session to time out or you can forceably end the session and empty the $_SESSION variable
<?php
//You might think its weird to start the session if we're going to end it,
//but if it wasn't here in the first place then how can we remove it, so, start/resume the session:
session_start();
//Now we'll empty the $_SESSION variable by returning it to its default, an empty array
$_SESSION = Array();
//Finally we can destroy the session and remove the session id from the server
session_destroy();
?>
i'll cover the other parts of sessions in a later tutorial, i hope this sets you up with the basics

