#native_company# #native_desc#

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)

$SES_VAR = array();

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

function sess_start()
 if (!$MYSID) 
    $MYSID = generate_SID();


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

    // if session is expired, then skip ...
    if ( time() > $arr[0] )

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

function sess_register($name)
 global $SES_VAR;

function sess_close()
        $SESSION_DIR, $expired_date;

 $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};

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

file : index.php

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

// register variables you want to store in persistent session :

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

print "
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
// must be started !!

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