basic sessions tutorial with register globals switched off
we're going to show you how you can make use of php sessions easily and quickly. we assume that you are familiar with the basics of php. the php session page and function list can be found here. you should have register globals switched off in your php configuration and for sessions not to use cookies. you can do this by editing php.ini and setting the below values like so.
register_globals off;
session.use_cookies 0;
or if you are using apache and have allow overrides switched on for your directory, you can create a .htaccess file to place in your current working directory.
now lets get on with it. the creation, deletion and checking of session variables are handled the same as you would create other variables.
first of all, you must start the session, this can be done with session_start().
session_start();
// php session has now been started
you may also wish to change the name of the session variable, this can be done with session_name().
session_name('my_name_here');
session_start();
on all your php pages that you wish to use sessions, you will need to include the above. if you change the name using session_name() you will need to keep that name consistent through all the pages if you want your session variables to remain in tact.
you now know how to start the session, it's time to start setting some variables. this is achieved the same way as you would create other variables within php, except for one difference. you need to use the $_SESSION array. this should become more clear in the following example.
session_name('s');
session_start();
// how you create the variable var usually in php
$var='var';
// how you create the session variable var
$_SESSION['var']='var'
you now have a session variable called var which can be access via $_SESSION['var']. if you want to create a session variable called foo, you change where it says var to foo, so for example
session_name('s');
session_start();
// session variable foo is now set
$_SESSION['foo']='foo';
you can store all types in sessions (such as arrays and objects) except the resource type. serialization and un serialization is done automatically so you don't need to worry about that.
session_name('s');
session_start();
// save an array
$_SESSION['myarray']=array(1,2,3,4,5,6,7,8,9);
from what you've learnt so far, you should be able to figure out the checking of session vars, weather they exist and how to delete them. it is done how you would do it with normal variables.
session_name('s');
session_start();
// check session variable is set
if(isset($_SESSION['foo']))
{
// session foo is set
echo "session variable foo holds a value of {$_SESSION['foo']}";
}
else
{
// session foo isn't set
$_SESSION['foo']='foo';
}
// delete a session variable
unset($_SESSION['my_session_var']);
// check the value of a session variable against some user input
if(isset($_SESSION['foo']&&isset($_GET['bar'])&&$_SESSION['foo']==$_GET['bar'])
{
echo "{$_SESSION['foo']} is equal to {$_GET['bar']}";
}
else
{
echo "{$_SESSION['foo']} is <b>not</b> equal to {$_GET['bar']}";
$_SESSION['foo']='bar';
}
that is pretty much all there is too it in using the sessions. one further thing is that if you want to pass our sessions from page to page (which you do, otherwise you wouldn't be using sessions), you need to pass the session id between pages.
this can be done automatically using cookies, but you configured php not to do this, so you need to pass the session id over the query string. when starting a session and you're not using cookies php creates a constant named SID. this holds bother the session name and session id in name=ID format. you can see this in the following example.
// check if name was set on the query string and set the session to that value
$_SESSION['name']=(isset($_GET['name']))?$_GET['name']:"didn't set a name";
// create a link to page to with our session id so we can get access to the session variables
echo '<a href=page2.php?',SID,'>page2</a>';
page2.php
session_name('s');
session_start();
echo "your name is: {$_SESSION['name']}";
// we don't need the variable no more, so we delete it
unset($_SESSION['name']);
|
this article has been rated 5.34 out of 804 votes. rate this article: