PHP Session: A simple tutorial for session

· Dev & Design
Authors

Besides cookies, there is one more way to pass information to different web-pages:
Sessions.

A session-enabled page allocates unique identifiers to the users the first time
they access the page, and then reassociates them with previously allocated ones when they return
to the page. Any global variables which were associated with the session will then become available to your code.


The difference between sessions and cookies

The main difference between sessions and cookies is that a session can hold multiple
variables, and you need not have to set cookies for every variable. By default, the
session data is stored in the cookie wich has an expiry date of zero, which means that
the session remains active only as long as the browser.Once you close the browser, all
the stored information is lost. This behavior can be modified by changing the �session.
cookie_lifetime� setting in �php.ini� from zero to whatever you want the cookie
lifetime to be.


How to Start a Session

Before starting to work with sessions, you must explicitly start a session
using �session_start()� function. If you want sessions to start themselfs
automatically, you should enable the �session.auto_start� setting in PHP�s
configuration file.

session_start();
//starts or resumes a function
print �Your session ID is: � . session_id();
//displays the session ID
session_destroy();
//ends the session; comment this line and
the browser will output the same session ID as before

After starting a session, you can access to the session ID via the
�session_id()� function. After completing the work, you can destroy the session
using �session_destroy()�.


Register Variables to a Session

the main objective of the session is to hold the values of variables. You must register
session variables using the �session_register()� function, before trying to read
them on a session-enabled page. Remember that a �session_register()� requires
you to pass as an argument “variable name”, and not the variable itself:

<?php
session_start();
?>
<html>
<body>
<?php
if(isset($stored_var))
{
print $stored_var; //this will not be displayed the first time you load the page, because you haven�t registered the variable yet!
}
else
{
$stored_var = �Hello from a stored variable!�;
session_register(�stored_var�); //don�t do this: session_register($session_var)
}
?>
</body>
</html>

you can test if a variable is assigned using the �isset()� function.


Remove the Registered Variables

To remove the registered variables, you need to use the session_unset()
function. This function when called destroys all variables associated with a session,
both in the the script and within session file .

<?php
session_start();
?>
<html>
<body>
<?php
session_register(“test”);
$test = 12;
print $test;//outputs 12
session_unset(); //$test is destroyed
session_destroy();
print $test; //outputs nothing
?>
</body>
</html>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s