PHP Session

PHP Session Print E-mail
Contributed by Joe   
Thursday, 06 July 2006

PHP Sessions overview

HTML and web servers by default don't keep track of information that was entered on a page when the client's browser loads another page. This makes doing anything that involves using the same information from a user on several pages difficult.

Sessions help solve this problem by maintaining data during a user's visit to your web site from page to page on your site. Each session can store many variables that are maintained throughout that session. The server keeps track of users' sessions by assigning them a unique session ID, generated by the server, when the session starts. This identifier is called the session identifier and must be sent to the server each time a page is requested once a session begins.

 

How does PHP session work?

Sessions are stored on the server. The session variables are stored in a file and are serialized. When a variable is serialized, it's written out to a file as its name, type, and value all in a sequential string. On a Unix-based server, this file is usually written out to a directory under the /tmp (temporary) filesystem.

The browser sends the session ID to the server each time it requests a page. The browser can send the session ID to the server either through a cookie or as a URL parameter. The default is to use the cookie, but because it's possible for a user to turn off cookies in his browser preferences, we also discuss passing the session ID in the URL string

Using PHP Sessions

To start a session, place the session_start function at the beginning of your PHP script before you can store or access any data during in the session.

PHP Sessions Example Simply starting a session

<?php
    session_start();
?>

 How to assing variables  a session?

To store and access session variables by the $_SESSION global variable with the name of the variable supplied within brackets. Assigning a new variable to the $_SESSION global automatically adds it to the session. The session must be started before you can access the session variables.

<?php
    session_start();
    $_SESSION['hello'] = 'Hello World';
    echo $_SESSION['hello'];
?>

Retrieve a php session value 

Now if the user was to follow a link to another page on your site that starts a session, the $_SESSION global variable contains a key called hello with the string value of Hello World.


<?php
    session_start();
    echo $_SESSION['hello'];
?>

Ending a Session

To end a session, use the session_destroy function

Setting a session's timeout

After a certain time period, it's reasonable to expect that a user's session should automatically log out, which is essentially an expiration period. PHP allows you to specifically set this duration. The best way to do this is to modify the .htaccess file.

The .htaccess file affects the HTML and PHP files in the same directory as the file. It allows you to make configuration changes without modifying Apache's configuration files. Any changes made in the .htaccess file also apply to files in subdirectories unless another .htaccess file is in a subdirectory.


<IfModule mod_php4.c>
  php_value session.gc_maxlifetime "14400"
</IfModule>
 

 

Related Articles:

 PHP Syntax

Basic PHP Tutorial

Last Updated ( Thursday, 06 July 2006 )

  home              contact us

 

©2006-2008 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground