ZoomCities IT Community Webmaster Forum

Full Version: [PHP] Simple Session Tutorial!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In this short tutorial i will be showing you the basics of PHP's session feature. You can reply and ask questions as you wish Biggrin


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 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

Code:
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 Code:
<?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 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 Code:
<?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

Thumbup, nice and clear. i used to have hard time understanding others session handling, but you give us the core... it seems that i think futher ahead i failed to see the obvious :blush:
now how about the file vs database session handling? Biggrin
and how about the session time?
Congratulation you two on becoming Active Mods. [/offtopic]

Could you add session_destroy(), session_name() and session_id()? Used quite alot and handy for certain things any chance you could add these?
There's a few security topics that you might want to cover with session_id(); I would do them separately.
update: Merged posts!

Lee Stevens Wrote:
update: Merged posts!


thanks lee Wink

An alternative to $_SESSION = Array(); would be to do:

PHP Code:
<?php
unset($_SESSION);
?>


Although I think when you call session_destroy() it automatically clears the array.

darkfate Wrote:
An alternative to $_SESSION = Array(); would be to do:

PHP Code:
<?php
unset($_SESSION);
?>


Although I think when you call session_destroy() it automatically clears the array.


yh i always used to but on the PHP site...

Quote:
Do not unset() on $_SESSION itself as this will disable the special function of the $_SESSION superglobal.

Ah. That makes sense I guess.
Thank you for this tutorial

:cheer Wink
Thank you for the brief info. I now fully understand the use of SESSION
Reference URL's