PHP - Sessions
Sessions allow us to save user "state" between subsequent visits to the page. Sessions and cookies are similar mechanisms. Both allow us to save data, which will be available to us at a later visit. Main difference is that session data is stored on a web server, while cookies resides on client machines. Because of this, sessions are capable of handling much more data than cookies.
Sessions work in the following way: when a user visits your web page, he stars a session and gets assigned a unique ID. This ID is called Session ID (SID). SID is sent to a user using a cookie.
When a session is running, you can register session variables and assign data to them. This is session data. At the end of a session, we will save user session state, which will save session data to a web server filesystem. At a next visits, user sends his cookie, web server recognizes visitor by its SID, and restores previously saved state.
Sessions behavior greatly depends of PHP configuration. One of the most important settings is a session lifetime. Session lifetime is a time during which the session is valid. Actually, it is the time during which session cookie exists.
When this time passes, web server will delete session data, and restoration of deleted session state will be impossible. Session lifetime in seconds can be checked with session_get_cookie_params(). This function will return an associative array with elements "lifetime", "path", "domain" and "secure". For a detailed description of these variables, see PHP manual, section about cookies. Yes, cookies. These are a cookie parameters. Similarly, you can change any of these parameters with session_set_cookie_params().
Sessions are commonly used in the authentication systems and site customization. You will see session usage in many of our examples.
In this example, we will set session cookie parameters, start a session, assign values and finally destroy this session.
// set the session cookie lifetime to one hour
// be sure to change parameters BEFORE startint session.
// I you start session first, session cookie will be
// sent with old parameters
session_set_cookie_params(3600);
// Since sessions use cookies, be sure to
// start session before any output.
session_start();
?>
<html>
<head>
<title>Sessions</title>
</head>
<body>
<div align="center">
<?php
if (!isset($_SESSION['sessName']))
{
?>
<h2>Hello. You are first time visitor</h2>
<p>Please, type in your name, and select your favorite language</p>
<form method="post" action="sess.php">
Your name:<input type="text" size="30" name="Name"><br>
Your language:
<select name="Language">
<option>English</option>
<option>French</option>
<option>Spanish</option>
</select><br>
<input type="submit" name="submit" >
</form>
<?php
}
else
{
?>
<h2>Hello, <?php echo $_SESSION['sessName']; ?></h2>
<p>You will see this webpage in <?php echo $_SESSION['sessLanguage']; ?> language.</p>
<p><a href="sess.php?delete">Click here to destroy your session</a></p>
<p>Session parameters:</p>
<pre>
<?php print_r(session_get_cookie_params()); ?>
</pre>
<?php
}
?>
</div>
</body>
</html>
Related Marakana Courses
- PHP and MySQL Bootcamp Training
September 21st, 2007 at 6:15 pm
That's a great tutorial. But what are your views about storing using memcache for sessions? I never tried and reading the manual on memcache at php.net at the moment.
October 7th, 2008 at 7:14 pm
Hi!
I would like make better my SQL knowledge.
I red that many SQL resources and want to
read more about SQL for my position as mysql database manager.
What would you recommend?
Thanks,
Werutz
November 1st, 2008 at 4:27 pm
Test message
Sorry me noob…
December 7th, 2008 at 3:10 pm
In most cases a product's rating went down, expanding the range between highest and lowest rated.Unlike Kaspersky, Symantec provides Norton users with little explanation of its features or settings, either in the configuration settings or on its technical support section. Also we don't like Norton's dependency on Internet Explorer to explain Help items or services provided by Symantec (windows pop up in IE even when Firefox is your default browser), or that fee-based services have once again crept into the technical support section. Having improved a lot
last year in Symantec's flagship antivirus product, it makes sense we'd see more modest enhancements for this year's Norton AntiVirus 2008. While Norton
AntiVirus.
December 18th, 2008 at 6:35 pm
Hello
As newly registered user i only want to say hi to everyone else who uses this forum
December 28th, 2008 at 6:20 pm
Hy my name is mnorgovudkka
Im from mongolia
Buy
January 6th, 2009 at 10:05 am
What is bumburbia?