#native_company# #native_desc#
#native_cta#

Persistent Session (Server-Side Cookie) with PHP

By Alexander Yanuar Koentjara
on January 22, 2001

Hello !!

If somoene wonder what is a persistent session or server-side cookie, then I will give you an introduction :

“Persistent Session is a session that is stored on your server, but the data will persist even when user disconect from your web server until the expired date comes”

This persistent session, some people called with “Server-Side Cookie” because how it works is really like cookie but it is stored in server side.

To simulate persistent session, we still need cookie (just like original Session technology does).
This cookie is not to store the data itself, but to store the SESSION ID which will be different among every users.

We also need a temporary directory in server to store the Session data with read/write permission (in UNIX/Linux, you can use chmod 777 to change the permission).

Ok, first you have to define the temp directory, for example : /temp/

Then, we can make persistent session management module :


file    : mod_PSession.php
purpose : module of Persistent Session
**************************************
<?
// your temp directory :
$SESSION_DIR = "/temp/";

// The expired date.
$expired_date = 30*3;  // 30 days times 3 (3 months)

srand((double)microtime()*1000000);
$SES_VAR = array();

function generate_SID()
 {
 return md5(rand(0,32000) . $REMOTE_ADDR . rand(0,32000));
 }

function sess_start()
 {
 global $MYSID, $CKDOMAIN, $SES_VAR, $SESSION_DIR;
 if (!$MYSID) 
    $MYSID = generate_SID();

 setcookie("MYSID",$MYSID,time()+365*24*3600,"/");

 if (file_exists($SESSION_DIR.$MYSID))
    {
    $arr = file($SESSION_DIR.$MYSID);

    // if session is expired, then skip ...
    if ( time() > $arr[0] )
         {
         unlink($SESSION_DIR.$MYSID); 
         return;
         }

    // read content of session from file system ... 
    for ($i=1;$i<count($arr);$i++)
        {
        $arr2=explode(":",$arr[$i]);
        $arr2[1]=base64_decode($arr2[1]);
        $SES_VAR[$arr2[0]]=1;
        GLOBAL ${$arr2[0]};
        ${$arr2[0]} = $arr2[1];
        }
    }
 }

function sess_register($name)
 {
 global $SES_VAR;
 $SES_VAR[$name]=1;
 }

function sess_close()
 {
 global $MYSID, $CKDOMAIN, $SES_VAR, 
        $SESSION_DIR, $expired_date;

 reset($SES_VAR);
 $fp = fopen($SESSION_DIR.$MYSID,"w");
 fputs($fp,(time() + $expired_date * 24 * 3600)."n");
 while (list($key,$val)=each($SES_VAR))
       {
       GLOBAL ${$key}; 
       $val = ${$key};
       fputs($fp,$key.":".base64_encode($val)."n");
       }
 fclose($fp);
 }
?>

Now, let’s try how to implement the module :


file : index.php
****************
<?
include("mod_PSession.php");

// sess_start must be called on the beginning of your code !!
sess_start();

// register variables you want to store in persistent session :
sess_register("myname");
sess_register("myage");
sess_register("mygender");

// now, put the values ...
$myname   = "Alexander";
$myage    = "22";
$mygender = "Male of course !!";

print "
<HTML>
<BODY>
Session is saved ! 
<A HREF='next.php'>Go to next page</A> </BODY> </HTML> "; // sess_close must be called after all is done sess_close(); ?>

In sequence :
1. You must include file mod_PSession.php.
2. You must call sess_start() to start the session support.
3. You must call sess_register() to register any variables that you want the session to remember.
4. You must call sess_close() if you want to save the session to file system. Usually, this is called in the last part of your code.

To see if the persistent session works, create the “next page” :


file : next.php
***************
<?
include("mod_PSession.php");
// must be started !!
sess_start();

print "<HTML><BODY>";
print "Hello !! My name is $myname, 
n"; print "I am $myage years old, and I am $mygender
n"; print "<A HREF='next.php'>Go to last page</A>" print "</BODY></HTML>"; // I increase my age ... :P // Each time you refresh the page, my age will be added $myage++; sess_close(); ?>

best regards,

Alexander Yanuar Koentjara
SYSTEM DEVELOPER
www.globalsources.com