sorry about that. in my hurry to share my code i forgot to test properly. here is an improved version of the code that i am using curently. It functions like the built in session management functions of PHP4.
FOR STORING SESSION IN FILE
/* include this file in all your php pages. if you can, auto_prepend this file.
every page that requiers session management should call sess_start() at the beggining
of the page and sess_close() at the end.
new variables are registered by calling
sess_register('variable_name');
this variable will be placed at the global scope in all pages that call sess_start()
and can be accessed like
echo $variable_name;
values assigned to sess variables will be stored only if the sess_close() function
is called at the end of the page
to unregister a sess variable call
sess_unregister('variable_name')
to destory a session call
sess_destroy()
*/
/ this will be the first few characters of the sess file created
example: $pre = 'sess_';
file = sess_00b3a0f85e339d98181db0de4aee1cc6
/
$pre = '';
/ the path to the dir where your sess files will be stored. make this directory world
writable. this directory should be outside your web server document root directory
example: $dir = '/tmp/';
NOTE: THE END SLASH IS REQUIRED
/
$dir = '/';
function createID() {
global $SERVER_NAME, $REMOTE_ADDR;
return md5($SERVER_NAME.$REMOTE_ADDR.time());
}
function sess_start() {
global $SESSID, $dir, $pre, $sid;
$defSID = true;
if(($sid) && !isset($SESSID))
$SESSID = $sid;
else
$defSID = false;
if($SESSID)
{
if(!file_exists($dir.$pre.$SESSID))
{
$tempID = createID();
setcookie('SESSID',$tempID,'','/','','');
$SESSID = $tempID;
$sessdata['init'] ='' ;
$sessdata = serialize($sessdata);
$fp = fopen($dir.$pre.$SESSID,'w+');
fwrite($fp, $sessdata);
fclose($fp);
define('SID','sid='.$tempID);
}
else
{
$fp = fopen($dir.$pre.$SESSID,'r');
$sessdata = fread($fp,1000000);
$sessdata = unserialize($sessdata);
while(list($n,$v) = @each($sessdata))
{
global $$n;
$$n = $v;
}
fclose($fp);
if($defSID)
define('SID','sid='.$SESSID);
else
define('SID','');
}
}
else
{
$tempID = createID();
$SESSID = $tempID;
setcookie('SESSID',$tempID,'','/','','');
$fp = fopen($dir.$pre.$SESSID,'w+');
$sessdata['init'] ='' ;
fwrite($fp, $sessdata);
fclose($fp);
define('SID','sid='.$tempID);
}
}
function sess_register($n) {
global $SESSID, $dir, $pre;
$fp = fopen($dir.$pre.$SESSID,'r');
$sessdata = fread($fp,1000000);
print $sessdata;
print "<br>";
$sessdata = unserialize($sessdata);
print gettype($sessdata);
$sessdata[$n] = '';
$sessdata = serialize($sessdata);
fclose($fp);
$fp = fopen($dir.$pre.$SESSID,'w+');
fwrite($fp,$sessdata);
fclose($fp);
}
function sess_close() {
global $SESSID, $dir, $pre;
if(file_exists($dir.$pre.$SESSID))
{
$fp = fopen($dir.$pre.$SESSID,'r');
$sessdata = fread($fp,1000000);
$sessdata = unserialize($sessdata);
fclose($fp);
while(list($n,$v) = @each($sessdata))
{
global $$n;
$sessdata[$n] = $$n;
}
$sessdata = serialize($sessdata);
$fp = fopen($dir.$pre.$SESSID,'w+');
fwrite($fp,$sessdata);
fclose($fp);
}
}
function sess_unregister($var) {
global $dir, $pre, $SESSID, $$var;
$fp = fopen($dir.$pre.$SESSID,'r');
$sessdata = fread($fp,100000);
fclose($fp);
$sessdata = unserialize($sessdata);
unset($sessdata[$var]);
unset($$var);
$sessdata = serialize($sessdata);
$fp = fopen($dir.$pre.$SESSID,'w+');
fwrite($fp,$sessdata);
fclose($fp);
}
function sess_destroy() {
global $SESSID, $pre, $dir;
if(file_exists($dir.$pre.$SESSID))
{
unlink($dir.$pre.$SESSID);
$SESSID = '';
unset($SESSID);
}
}
FOR STORING IN MYSQL DATABASE
/*
include this file in all your php pages. if you can, auto_prepend this file.
create this table in your database
create table session {
ID varchar(32) NOT NULL,
cdate timestamp(12),
session text,
PRIMARY KEY (ID),
INDEX cdate
}
every page that requiers session management should call sess_start() at the beggining
of the page and sess_close() at the end.
new variables are registered by calling
sess_register('variable_name');
this variable will be placed at the global scope in all pages that call sess_start()
and can be accessed like
echo $variable_name;
values assigned to sess variables will be stored only if the sess_close() function
is called at the end of the page
to unregister a sess variable call
sess_unregister('variable_name')
to destory a session call
sess_destroy()
*/
$hostname = '';
$user = '';
$pass = '';
$dbname = '';
$con = mysql_pconnect($hostname,$user,$pass);
$dblink = mysql_select_db($dbname)
function createID() {
global $SERVER_NAME, $REMOTE_ADDR;
return md5($SERVER_NAME.$REMOTE_ADDR.time());
}
function sess_start() {
global $SESSID, $sid;
$defSID = true;
if(($sid) && !isset($SESSID))
$SESSID = $sid;
else
$defSID = false;
if($SESSID)
{
$qhandle = mysql_query("select count(*) as num, session from session where ID = '$SESSID' group by session");
$qresult = mysql_fetch_array($qhandle);
$num = $qresult['num'];
if(!$num)
{
$tempID = createID();
setcookie('SESSID',$tempID,0,'/','',0);
define('SID','sid='.$tempID);
$sessdata['init'] = '';
$sessdata = serialize($sessdata);
mysql_query("insert into session values ('$tempID',null,'$sessdata')");
}
else
{
$sessdata = unserialize(stripslashes($qresult['session']));
while(list($n,$v) = @each($sessdata))
{
global $$n;
$$n = $v;
}
if($defSID) define('SID','sid='.$SESSID);
}
}
else
{
$tempID = createID();
setcookie('SESSID',$tempID,0,'/','',0);
define('SID','sid='.$tempID);
$sessdata['init'] = '';
$sessdata = serialize($sessdata);
mysql_query("insert into session ('$tempID',null,'$sessdata')");
}
}
function sess_register($n) {
global $SESSID;
$qhandle = mysql_query("select session from session where ID = '$SESSID'");
$qresult = mysql_fetch_array($qhandle);
$sessdata = unserialize(stripslashes($qresult['session']));
$sessdata[$n] = '';
$sessdata = serialize($sessdata);
mysql_query("update session set session = '$sessdata' where ID = '$SESSID'");
}
function sess_close() {
global $SESSID;
$qhandle = mysql_query("select session from session where ID = '$SESSID'");
$qresult = mysql_fetch_array($qhandle);
$sessdata = unserialize(stripslashes($qresult['session']));
while(list($n,$v) = @each($sessdata))
{
global $$n;
$sessdata[$n] = $$n;
}
$sessdata = serialize($sessdata);
mysql_query("update session set session = '$sessdata' where ID = '$SESSID'");
}
function sess_unregister($var) {
global $SESSID, $$var;
$qhandle = mysql_query("select session from session where ID = '$SESSID'");
$qresult = mysql_fetch_array($qhandle);
$sessdata = unserialize(stripslashes($qresult['session']));
unset($sessdata[$var]);
unset[$$var];
$sessdata = serialize($sessdata);
mysql_query("update session set session = '$sessdata' where ID = '$SESSID'");
}
function sess_destroy() {
global $SESSID;
mysql_query("delete from session where ID = '$SESSID'");
unset($SESSID);
}
let me know if you find any problems with this one
bishnu