Jump to content

Problem with session.php


kporter.porter

Recommended Posts

Session.php is not creating multiple entries into my Database.  Therefore, when someone logs in, it shows the last persons name and what info they have.

 

I need for each session to be seperate, I may have dozens of people logged in at any one time.  Here is the code:

 

<?php

/*
Functions for session management.
Author: Rikul Patel 
Initial Version: 6/27/2000 6:16AM 

SQL Table Schema:
create session 
(
	sid char(32) not null,
	data text,
	access timestamp default 'now()',
	primary key (sid)
);

Public functions:
void SessionStart([string sessionName]) - called to start session and import vars associated with session
void SessionEnd([string sessionName]) - called to end session and export vars associated with session
mixed SetSessionVar(string varName, mixed varValue,[string sessionName]) - set value of varName to varValue
mixed GetSessionVar(string varName, [string sessionName]) - returns value of varName
string GetSid([string sessionName]) - returns the session identifier. use to set cookie or URL propagation.


Default value of "sessionName" is "sid". So set cookie for "sid" to value of GetSid(). You can use
any types of variable to store in database except for object.


Revisions:
8/23/00 12:19AM		Made "TimeLimit" (expire limit in seconds) var global 
					Added documentation for SQL schema for session table
					Added some documentation of public functions for module


*/

$_SESSION_DATA=array();
$TimeLimit=600; // expired unused sessions in 10 minutes

function _SessionCreate($SessionName)
{
$Db=new sql;
global $_SESSION_DATA;

$GLOBALS["$SessionName"]=md5 (microtime() . $_SERVER['REMOTE_ADDR']);
$Db->exec("insert into session (sid) values ('" . $_SESSION["$SessionName"] . "')");        
$_SESSION_DATA["$SessionName"]=array();
SetCookie ("$SessionName", $_SESSION["$SessionName"],0,"/");
return;         
}

function _RemoveOldSession()
{
global $TimeLimit;
$Db=new sql;

$Db->exec("delete from session where now()-access > interval '$TimeLimit seconds' ");
return;
}

function SessionStart($SessionName="sid")
{       
if(@$GLOBALS["_SESSION_STATE"] != "STARTED")
{
	$GLOBALS["_SESSION_STATE"] = "STARTED";
	_RemoveOldSession(); 
	$Db=new sql;  
	$Db->exec("select * from session where sid='" . @$GLOBALS["$SessionName"] . "'");
	if (! $Db->NumRows()) {
		_SessionCreate($SessionName);
	} else {
		global $_SESSION_DATA;
		$r=$Db->next();
		$_SESSION_DATA["$SessionName"]=unserialize(base64_decode(@$r["data"]));
		$Db->exec("update session set access=now() where sid='" . GetSid() . "'");
	}                 
	return;
}
}

function SessionEnd($SessionName="sid")
{
if($GLOBALS["_SESSION_STATE"] != "ENDED")
{
	$GLOBALS["_SESSION_STATE"] = "ENDED";
	$Db=new Sql;
	global $_SESSION_DATA;
	$_SData=base64_encode(serialize($_SESSION_DATA["$SessionName"]));        
	$Db->exec("update session set data='$_SData', access=now() where sid='" . GetSid() . "'");
	return;
}
}

function GetSessionVar($VarName, $SessionName="sid")
{
global $_SESSION_DATA;
return @$_SESSION_DATA["$SessionName"]["$VarName"];
}

function SetSessionVar($VarName, $VarValue, $SessionName="sid")
{
global $_SESSION_DATA;
return ($_SESSION_DATA["$SessionName"]["$VarName"]=$VarValue);
}

function DelSessionVar($VarName, $SessionName="sid")
{
global $_SESSION_DATA;
unset ($_SESSION_DATA["$SessionName"]["$VarName"]);
}

function GetSid($SessionName="sid")
{
return $GLOBALS["$SessionName"];        
}
?>

 

When I look at my DB table it has the OID SID Data and Timestamp, but SID is blank.  So it is not recording the SID and can't differientiate among the sessions.  There is only one entry.

 

Let me know if you need more info.  Thanks for the help.

 

Kevin Porter

Link to comment
Share on other sites

Trying to learn PHP and fix someone else's code from 5-7 years ago.  So not certain on all the code.

 

What is supposed to happen is the person logs in, it creates a session id, then I call that session id from throughout the site to pop up their name saying welcome and to show them only those forms and products they are supposed to see.

 

But I am not capturing the SID, which is what I am using to tell the difference between active seesions.  So this is supposed to capture (or create) the SID, save it to the DB.  Then delete the entry when it times out or they close the browser.

 

 

Link to comment
Share on other sites

Is session.php a standard file that PHP knows to look for or am I calling it from some where?

 

The _CreateSession code looks as though it is grabbing the time and supposedly the the ip address of the machine logging in.  It then uses this to create a cookie and looks for that cookie when loading other pages, allowing it to know what info to display by matching the cookie to the DB entry.

 

function _SessionCreate($SessionName)
{
$Db=new sql;
global $_SESSION_DATA;

$GLOBALS["$SessionName"]=md5 (microtime() . $_SERVER['REMOTE_ADDR']);
$Db->exec("insert into session (sid) values ('" . $GLOBALS["$SessionName"] . "')");        
$_SESSION_DATA["$SessionName"]=array();
SetCookie ("$SessionName", $GLOBALS["$SessionName"],0,"/");
return; 

 

It just doesn't seem to be writing anything into the DB, so every one gets the same cookie and sees the name of whoever the person before them is that logged in.

 

I am guessing that it is not finding a value for $GLOBALS["SessionName"] which is what is causing my problem.  I was able to use the SQL statement to put something in the sid column.  What would be a good print statement to use on the display page to try and see what value it is giving for:

 

$GLOBALS["$SessionName"]

(microtime()

$_SERVER['REMOTE_ADDR'])

 

So I can try to see what value it is not getting.

 

Thanks.

 

 

Link to comment
Share on other sites

Ok.  I printed out "md5 (microtime() . $_SERVER['REMOTE_ADDR']);" and got a good answer on the page, I also printed the IP address and Microtime seperately, together and MD5'd.

 

But I get nothing when I try to print "$GLOBALS["$SessionName"]" which is supposed to equal "md5 (microtime() . $_SERVER['REMOTE_ADDR']);".

 

Therefore, nothing is being put into the SID column.  Any idea why these are not equaling each as they should?

 

function _SessionCreate($SessionName)
{
$Db=new sql;
global $_SESSION_DATA;

$GLOBALS["$SessionName"]=md5 (microtime() . $_SERVER['REMOTE_ADDR']);
$Db->exec("insert into session (sid) values ('" . $GLOBALS["$SessionName"] . "')");        
$_SESSION_DATA["$SessionName"]=array();
SetCookie ("$SessionName", $GLOBALS["$SessionName"],0,"/");
return;

 

On the DB exec line, why is there a period before and after "$GLOBALS["$SessionName"]".  Could this be causing the problem?

 

Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.