session_set_save_handler()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

session_set_save_handler()

Post by mikusan »

Hi there, i am building my own handler because i need my sessions to be stored in the database so that i can also compare them with cookies and what not. Though I already have something, it is very basic and i have not tested it yet, i was wondering if someone has already done one and/pr would give me some direction on how to make a good one.
I am closely following the manual's technique, that is:

Code: Select all

<?php
function open ($save_path, $session_name) {
  global $sess_save_path, $sess_session_name;
       
  $sess_save_path = $save_path;
  $sess_session_name = $session_name;
  return(true);
}

function close() {
  return(true);
}

function read ($id) {
  global $sess_save_path, $sess_session_name;

  $sess_file = "$sess_save_path/sess_$id";
  if ($fp = @fopen($sess_file, "r")) {
    $sess_data = fread($fp, filesize($sess_file));
    return($sess_data);
  } else {
    return(""); // Must return "" here.
  }

}

function write ($id, $sess_data) {
  global $sess_save_path, $sess_session_name;

  $sess_file = "$sess_save_path/sess_$id";
  if ($fp = @fopen($sess_file, "w")) {
    return(fwrite($fp, $sess_data));
  } else {
    return(false);
  }

}

function destroy ($id) {
  global $sess_save_path, $sess_session_name;
       
  $sess_file = "$sess_save_path/sess_$id";
  return(@unlink($sess_file));
}

/*********************************************
 * WARNING - You will need to implement some *
 * sort of garbage collection routine here.  *
 *********************************************/
function gc ($maxlifetime) {
  return true;
}

session_set_save_handler ("open", "close", "read", "write", "destroy", "gc");

session_start();

// proceed to use sessions normally

?>
And i have replaced most file read write stuff with mysql_query()
I doubt that what i got so far is even worth reading so i was hoping to get insight or even some link to somewhere that has previously helped some to achieve this.

here is my mysql table structure, in short:
sess_id
sess_time timestamp(14)
user
data

thx
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post by Zoram »

Just a suggestion but why don't you test out the code, then if you run into a problem post it here and people might be able to help you... but when you haven't tested the code it doesn't really do you any good to learn... you'll just be asking for others to write the whole thing for you... no offense ment.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

No offense taken, I didn't mean to ask for the code, but when i wrote the message i was really busy with other work and was hoping someone would gimme a jump start with a link to a website or something....I will test out my code and get back at you....
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

So here it is... unfortunately i have no permissions to see the error logs so i am totally blind and unsure why this doesn't work. For one thing my website is extremely modular, and as such i would like to include this as a module, i learned, correct me if i am wrong that i cannot make functions within functions so my only way out in this case is to make an include, fair enough. Here is the code, that i think is really bad, and since i am learning i am hoping to make this code nice ;)

Code: Select all

function sess_open ($session_path, $session_name) 
{
  	return(true);
}

function sess_close() 
{
  	return(true);
}

function sess_read ($id) 
{
	DB_connect();	

	$SQL = mysql_query("SELECT * FROM tr_sessions WHERE sess_id=$id");
	$rows = mysql_num_rows($SQL);
	
	if($rows == 1)
	{
		$Data = mysql_fetch_array($SQL);
		return($Data['data']);
	}

  	else 
	{
    	return(""); // Must return "" here.
  	}

}

function sess_write ($id, $sess_data)
{
	DB_connect();

	$query = "UPDATE tr_sessions SET data = $sess_data";

	if (isset($PHP_AUTH_USER))
	{
		$query .= ", user=$PHP_AUTH_USER";
	}
	
	$query .= " WHERE sess_id=$id";
	
	if (mysql_affected_rows())
	{
		return(true);
	}

	//If it got so far then an entry was not made, create one now
	$query = "INSERT tr_sessions SET data=$sess_data, sess_id=$id";

	else 
	{
    	return(false);
  	}

}

function sess_destroy ($id) 
{
	DB_connect();

	$query = "DELETE tr_sessions WHERE sess_id=$id";

	if($result = mysql_query($query)
	{
		return(true);
	}
	
	else
	{
		return(false);
	}

}

function sess_gc ($maxlifetime) 
{
	//Leave garbage collection until later
  	return true;
}

session_set_save_handler ("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_start();
and this is my table structure...

sess_id int(10)
start_time timestamp(14)
remote_ip varchar(15)
uid mediumint(8)
data text
user
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Well i have worked hard on the code again, and i got a much cleaner and more coherent code, yet i am still not able to write my data nor session id nor anything to the databse...

Code: Select all

function sess_open ($session_path, $session_name) 
{
	site_log('opened session');
  	return(true);
}

function sess_close() 
{
	site_log('closed session');
  	return(true);
}

function sess_read ($id) 
{
	site_log('read session');
	DB_connect();

	$SQL = mysql_query("SELECT * FROM tr_sessions WHERE sess_id='$id'");
	if (mysql_num_rows($SQL) == 1)
	{
		$Data = mysql_fetch_assoc($SQL);
		return($Data['data']);
	}

  	else 
	{
    	return(""); // Must return "" here.
  	}
	
}

function sess_write ($id, $sess_data)
{
	site_log('write session');
	DB_connect();

	$query = "UPDATE tr_sessions SET data='$sess_data'";

	if (isset($PHP_AUTH_USER))
	{
		$query .= ", user='$PHP_AUTH_USER' WHERE sess_id='$id'";
	}
	
	//$query .= " WHERE sess_id='$id'";
	
	if (mysql_affected_rows())
	{
		return(true);
	}
	
//If it got so far then an entry was not made, create one now
		$query = "INSERT tr_sessions SET data='$sess_data', sess_id='$id'";
	else 
	{
		
    	return(false);
  	}
}

function sess_destroy ($id) 
{
	site_log('destroy session');
	DB_connect();
	
	$query = "DELETE FROM tr_sessions WHERE sess_id='$id'";

	if($result = mysql_query($query))
	{
		return(true);
	}

	else
	{
		return(false);
	}

}

function sess_gc ($maxlifetime) 
{
	//Leave garbage collection until later
  	return true;
}

session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_start();
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Nevermind, apart some sensless if statements it works miracles...

strange though, am i the only one interested in writing my own session handling???

it's an awesome tool once you get the 100 lines of code ;)
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

I have also wrote my own. I needed to because I needed to sync the session files across to the other webserver. I am using ftp to keep the two sites synchronized.
Post Reply