RE: [PHP-DB] php sessions using mysql (or any db)
On Mon, 18 Nov 2002, Adam Nelson wrote: > One thing I left out - I had to take out the $GLOBALS[acdb] part from > mysql_query. This doesn't work with my version of php (4.2.3) I guess. > Also, assert(!empty($SessionTableName)); didn't work, so I commented it > out. Whoops, the $GLOBALS[acdb] was legacy code for the site I was using. Ignore it. Sorry! Your SessionTableName is set in php.ini -- if $sessiontablename is empty, there is an issue that will prevent sessions from being written/read from! Peter > > -Original Message- > > From: Peter Beckman [mailto:[EMAIL PROTECTED]] > > Sent: Monday, November 18, 2002 12:01 PM > > To: Adam Nelson > > Cc: [EMAIL PROTECTED]; 'Rasmus Lerdorf' > > Subject: RE: [PHP-DB] php sessions using mysql (or any db) > > > > > > >// This code is released under the same license as > > PHP. (http://www.php.net/license.html) > >assert(get_cfg_var("session.save_handler") == "user"); > >// Without save_handler being set to user, everything > > works fine until it calls the write handler. > >$SessionTableName = get_cfg_var("session.save_path"); > > // [$database.]tablename > >' > >function db_error_message() { > > return mysql_error(); > >} > >function mysql_session_open ($save_path, $session_name) { > >return true; > >} > >function mysql_session_close() { > >return true; > >} > >function mysql_session_read ($SessionID) { > >global $SessionTableName; > >$SessionID = addslashes($SessionID); > >$session_data = mysql_query("SELECT Data FROM > > $SessionTableName WHERE SessionID = '$SessionID'",$GLOBALS[acdb]) or > > die(db_error_mess > > age()); > >if (mysql_numrows($session_data) == 1) { > >return mysql_result($session_data, 0); > >} else { > >return false; > >} > >} > >function mysql_session_write ($SessionID, $val) { > >global $SessionTableName; > >$SessionID = addslashes($SessionID); > >$val = addslashes($val); > >$SessionExists = mysql_result(mysql_query("SELECT > > COUNT(*) FROM $SessionTableName WHERE SessionID = > > '$SessionID'",$GLOBALS[acdb]), > > 0 > > ); > >if ($SessionExists == 0) { > >$retval = mysql_query("INSERT INTO > > $SessionTableName (SessionID, LastActive, Data) VALUES ('$SessionID', > > > > UNIX_TIMESTAMP(NOW()),'$val')",$GLOBALS[acdb]) or > > die(db_error_message()); > >} else { > >$retval = mysql_query("UPDATE > > $SessionTableName SET Data = '$val', LastActive = > > UNIX_TIMESTAMP(NOW()) WHERE SessionID = > >'$SessionID'",$GLOBALS[acdb]) or > > die(db_error_message()); > >if (mysql_affected_rows() < 0) { > >error_log("unable to update session data > > for session $SessionID"); > >} > >} > >return $retval; > >} > >function mysql_session_destroy ($SessionID) { > >global $SessionTableName; > >$SessionID = addslashes($SessionID); > >$retval = mysql_query("DELETE FROM > > $SessionTableName WHERE SessionID = > > '$SessionID'",$GLOBALS[acdb]) or die(db_error_message()); > >return $retval; > >} > >function mysql_session_gc ($maxlifetime = 604800) { > >global $SessionTableName; > >$CutoffTime = time() - $maxlifetime; > >$retval = mysql_query("DELETE FROM > > $SessionTableName WHERE LastActive < > > $CutoffTime",$GLOBALS[acdb]) or die(db_error_message()); > >return $retval; > >} > >session_set_save_handler ( > >'mysql_session_open', > >'mysql_session_close', > >'mysql_session_read', > >'mysql_session_write', > >'mysql_session_destroy', > >'mysql_session_gc' > >); > > ?> > > On Mon, 18 Nov 2002, Adam Nelson wrote: > > > > > Thanks for th
RE: [PHP-DB] php sessions using mysql (or any db)
number 1 is perfect for one server number 2 (no jokes please) is great for multiple servers. What's neat is that you can use the format of number 1 with the mysql db in the back end quite simply. I could put the full code up with explanations, but here might not be the best place (It would take 2-3 pages). I think it should be in the documentation itself. I will write it up and send it to the appropriate parties and hopefully they will put it up. -Original Message- From: Adam Royle [mailto:[EMAIL PROTECTED]] Sent: Monday, November 18, 2002 5:12 PM To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: RE: [PHP-DB] php sessions using mysql (or any db) Hi, Just wondering, which is the better method: 1. Using PHP sessions ($_SESSION['var'] = "val";) 2. Using mySQL-based sessions (as described in this thread) I know if you're using multiple servers, a DB-based session would be handy. Any comments, anyone? Adam
RE: [PHP-DB] php sessions using mysql (or any db)
Hi, Just wondering, which is the better method: 1. Using PHP sessions ($_SESSION['var'] = "val";) 2. Using mySQL-based sessions (as described in this thread) I know if you're using multiple servers, a DB-based session would be handy. Any comments, anyone? Adam
RE: [PHP-DB] php sessions using mysql (or any db)
This works great :-) Thanks. I'll write a letter to onlamp.com to get that misleading code off their website (maybe it's PHP3 based?). Perhaps this should be added to PEAR? It appears that this is the proper repository for such things. I'll look into it although I would be much obliged if somebody in the know could handle this since I am new to PHP. One thing I left out - I had to take out the $GLOBALS[acdb] part from mysql_query. This doesn't work with my version of php (4.2.3) I guess. Also, assert(!empty($SessionTableName)); didn't work, so I commented it out. > -Original Message- > From: Peter Beckman [mailto:[EMAIL PROTECTED]] > Sent: Monday, November 18, 2002 12:01 PM > To: Adam Nelson > Cc: [EMAIL PROTECTED]; 'Rasmus Lerdorf' > Subject: RE: [PHP-DB] php sessions using mysql (or any db) > > > // This code is released under the same license as > PHP. (http://www.php.net/license.html) >assert(get_cfg_var("session.save_handler") == "user"); >// Without save_handler being set to user, everything > works fine until it calls the write handler. >$SessionTableName = get_cfg_var("session.save_path"); > // [$database.]tablename >' >function db_error_message() { > return mysql_error(); >} >function mysql_session_open ($save_path, $session_name) { >return true; >} >function mysql_session_close() { >return true; >} >function mysql_session_read ($SessionID) { >global $SessionTableName; >$SessionID = addslashes($SessionID); >$session_data = mysql_query("SELECT Data FROM > $SessionTableName WHERE SessionID = '$SessionID'",$GLOBALS[acdb]) or > die(db_error_mess > age()); >if (mysql_numrows($session_data) == 1) { >return mysql_result($session_data, 0); >} else { >return false; >} >} >function mysql_session_write ($SessionID, $val) { >global $SessionTableName; >$SessionID = addslashes($SessionID); >$val = addslashes($val); >$SessionExists = mysql_result(mysql_query("SELECT > COUNT(*) FROM $SessionTableName WHERE SessionID = > '$SessionID'",$GLOBALS[acdb]), > 0 > ); >if ($SessionExists == 0) { >$retval = mysql_query("INSERT INTO > $SessionTableName (SessionID, LastActive, Data) VALUES ('$SessionID', > > UNIX_TIMESTAMP(NOW()),'$val')",$GLOBALS[acdb]) or > die(db_error_message()); >} else { >$retval = mysql_query("UPDATE > $SessionTableName SET Data = '$val', LastActive = > UNIX_TIMESTAMP(NOW()) WHERE SessionID = >'$SessionID'",$GLOBALS[acdb]) or > die(db_error_message()); >if (mysql_affected_rows() < 0) { >error_log("unable to update session data > for session $SessionID"); >} >} >return $retval; >} >function mysql_session_destroy ($SessionID) { >global $SessionTableName; >$SessionID = addslashes($SessionID); >$retval = mysql_query("DELETE FROM > $SessionTableName WHERE SessionID = > '$SessionID'",$GLOBALS[acdb]) or die(db_error_message()); >return $retval; >} >function mysql_session_gc ($maxlifetime = 604800) { >global $SessionTableName; >$CutoffTime = time() - $maxlifetime; >$retval = mysql_query("DELETE FROM > $SessionTableName WHERE LastActive < > $CutoffTime",$GLOBALS[acdb]) or die(db_error_message()); >return $retval; >} >session_set_save_handler ( >'mysql_session_open', >'mysql_session_close', >'mysql_session_read', >'mysql_session_write', >'mysql_session_destroy', >'mysql_session_gc' >); > ?> > On Mon, 18 Nov 2002, Adam Nelson wrote: > > > Thanks for the clarification with the returns. I made the changes > > however and it still doesn't work. This code comes pretty > much straight > > off the presses from http://www.onlamp.com/lpt/a/832 > > > > What I have is quite modified from what is on there as it > is quite buggy > > to begin with. Anyway, It still doesn't
RE: [PHP-DB] php sessions using mysql (or any db)
http://www.php.net/license.html) assert(get_cfg_var("session.save_handler") == "user"); // Without save_handler being set to user, everything works fine until it calls the write handler. $SessionTableName = get_cfg_var("session.save_path"); // [$database.]tablename assert(!empty($SessionTableName)); function db_error_message() { return mysql_error(); } function mysql_session_open ($save_path, $session_name) { return true; } function mysql_session_close() { return true; } function mysql_session_read ($SessionID) { global $SessionTableName; $SessionID = addslashes($SessionID); $session_data = mysql_query("SELECT Data FROM $SessionTableName WHERE SessionID = '$SessionID'",$GLOBALS[acdb]) or die(db_error_mess age()); if (mysql_numrows($session_data) == 1) { return mysql_result($session_data, 0); } else { return false; } } function mysql_session_write ($SessionID, $val) { global $SessionTableName; $SessionID = addslashes($SessionID); $val = addslashes($val); $SessionExists = mysql_result(mysql_query("SELECT COUNT(*) FROM $SessionTableName WHERE SessionID = '$SessionID'",$GLOBALS[acdb]), 0 ); if ($SessionExists == 0) { $retval = mysql_query("INSERT INTO $SessionTableName (SessionID, LastActive, Data) VALUES ('$SessionID', UNIX_TIMESTAMP(NOW()),'$val')",$GLOBALS[acdb]) or die(db_error_message()); } else { $retval = mysql_query("UPDATE $SessionTableName SET Data = '$val', LastActive = UNIX_TIMESTAMP(NOW()) WHERE SessionID = '$SessionID'",$GLOBALS[acdb]) or die(db_error_message()); if (mysql_affected_rows() < 0) { error_log("unable to update session data for session $SessionID"); } } return $retval; } function mysql_session_destroy ($SessionID) { global $SessionTableName; $SessionID = addslashes($SessionID); $retval = mysql_query("DELETE FROM $SessionTableName WHERE SessionID = '$SessionID'",$GLOBALS[acdb]) or die(db_error_message()); return $retval; } function mysql_session_gc ($maxlifetime = 604800) { global $SessionTableName; $CutoffTime = time() - $maxlifetime; $retval = mysql_query("DELETE FROM $SessionTableName WHERE LastActive < $CutoffTime",$GLOBALS[acdb]) or die(db_error_message()); return $retval; } session_set_save_handler ( 'mysql_session_open', 'mysql_session_close', 'mysql_session_read', 'mysql_session_write', 'mysql_session_destroy', 'mysql_session_gc' ); ?> On Mon, 18 Nov 2002, Adam Nelson wrote: > Thanks for the clarification with the returns. I made the changes > however and it still doesn't work. This code comes pretty much straight > off the presses from http://www.onlamp.com/lpt/a/832 > > What I have is quite modified from what is on there as it is quite buggy > to begin with. Anyway, It still doesn't work. In perl, this would have > been done already. I can't be the first person to try running sessions > on a database. Does anyone have a session.inc file that would be > appropriate for me. I feel like it should just be in the php base > files. > > > > -Original Message- > > From: Rasmus Lerdorf [mailto:[EMAIL PROTECTED]] > > Sent: Friday, November 15, 2002 4:59 PM > > To: Adam Nelson > > Cc: [EMAIL PROTECTED] > > Subject: Re: [PHP-DB] php sessions using mysql (or any db) > > > > > > > CREATE TABLE `SessionsTable` ( > > > `SID` varchar(32) NOT NULL default '', > > > > This doesn't need to be a varchar. The sid will always be 32 > > chars, so > > make it a char(32) > > > > This is just an artifact of InnoDB/Mysql. I don't know the specifics, > but I think char is no longer used by InnoDB (ie. it is just an alias > for varchar). Anyway, I put in char, the ddl pops out as varchar. > > > > `expiration` int(11) NOT NULL default '0', > > > > I would suggest using a "timestamp" type here so MySQL will handle > > updating it for you automatically. > > timestamp would change with every update. This is a field to describe > when the record should expire > &
RE: [PHP-DB] php sessions using mysql (or any db)
Thanks for the clarification with the returns. I made the changes however and it still doesn't work. This code comes pretty much straight off the presses from http://www.onlamp.com/lpt/a/832 What I have is quite modified from what is on there as it is quite buggy to begin with. Anyway, It still doesn't work. In perl, this would have been done already. I can't be the first person to try running sessions on a database. Does anyone have a session.inc file that would be appropriate for me. I feel like it should just be in the php base files. > -Original Message- > From: Rasmus Lerdorf [mailto:[EMAIL PROTECTED]] > Sent: Friday, November 15, 2002 4:59 PM > To: Adam Nelson > Cc: [EMAIL PROTECTED] > Subject: Re: [PHP-DB] php sessions using mysql (or any db) > > > > CREATE TABLE `SessionsTable` ( > > `SID` varchar(32) NOT NULL default '', > > This doesn't need to be a varchar. The sid will always be 32 > chars, so > make it a char(32) > This is just an artifact of InnoDB/Mysql. I don't know the specifics, but I think char is no longer used by InnoDB (ie. it is just an alias for varchar). Anyway, I put in char, the ddl pops out as varchar. > > `expiration` int(11) NOT NULL default '0', > > I would suggest using a "timestamp" type here so MySQL will handle > updating it for you automatically. timestamp would change with every update. This is a field to describe when the record should expire > > > function mysql_session_open($session_path, $session_name) { > > > > mysql_pconnect("localhost", "root", "") > > or die("Can't connect to MySQL server! "); > > > > mysql_select_db("globalDB") > > or die("Can't select MySQL sessions database"); > > > > } // end mysql_session_open() > > You need to return true; at the end of this. > true. > > function mysql_session_close() { > > > > return 1; > > No, use return true; > > > function mysql_session_select($SID) { > > > > GLOBAL $sess_db; > > GLOBAL $sess_table; > > > > $query = "SELECT value FROM $sess_table > > WHERE SID = '$SID' AND > > expiration > ". time(); > > > > $result = mysql_query($query); > > > > } // end mysql_session_select() > > Uh, you need to return the actual value here or an empty string on an > error. > > > function mysql_session_write($SID,$value) { > > > > GLOBAL $sess_db; > > GLOBAL $sess_table; > > GLOBAL $lifetime; > > > > $expiration = time() + $lifetime; > > > > $query = "INSERT INTO $sess_table > > VALUES('$SID', '$expiration', '$value')"; > > > > $result = mysql_query($query); > > > > if (! $result) : > > > >$query = "UPDATE $sess_table SET > >expiration = '$expiration', > >value = '$value' WHERE > >SID = '$SID' AND expiration >". time(); > >$result = mysql_query($query); > > > > endif; > > > > } // end mysql_session_write() > > Again, you *must* return true; on a sucessful write. > > > function mysql_session_destroy($sessionID) { > > > > GLOBAL $sess_table; > > > > $query = "DELETE FROM $sess_table > > WHERE SID = '$sessionID'"; > > $result = mysql_query($query); > > > > } // end mysql_session_destroy() > > return true; > > -Rasmus > > > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] php sessions using mysql (or any db)
> CREATE TABLE `SessionsTable` ( > `SID` varchar(32) NOT NULL default '', This doesn't need to be a varchar. The sid will always be 32 chars, so make it a char(32) > `expiration` int(11) NOT NULL default '0', I would suggest using a "timestamp" type here so MySQL will handle updating it for you automatically. > function mysql_session_open($session_path, $session_name) { > > mysql_pconnect("localhost", "root", "") > or die("Can't connect to MySQL server! "); > > mysql_select_db("globalDB") > or die("Can't select MySQL sessions database"); > > } // end mysql_session_open() You need to return true; at the end of this. > function mysql_session_close() { > > return 1; No, use return true; > function mysql_session_select($SID) { > > GLOBAL $sess_db; > GLOBAL $sess_table; > > $query = "SELECT value FROM $sess_table > WHERE SID = '$SID' AND > expiration > ". time(); > > $result = mysql_query($query); > > } // end mysql_session_select() Uh, you need to return the actual value here or an empty string on an error. > function mysql_session_write($SID,$value) { > > GLOBAL $sess_db; > GLOBAL $sess_table; > GLOBAL $lifetime; > > $expiration = time() + $lifetime; > > $query = "INSERT INTO $sess_table > VALUES('$SID', '$expiration', '$value')"; > > $result = mysql_query($query); > > if (! $result) : > >$query = "UPDATE $sess_table SET >expiration = '$expiration', >value = '$value' WHERE >SID = '$SID' AND expiration >". time(); >$result = mysql_query($query); > > endif; > > } // end mysql_session_write() Again, you *must* return true; on a sucessful write. > function mysql_session_destroy($sessionID) { > > GLOBAL $sess_table; > > $query = "DELETE FROM $sess_table > WHERE SID = '$sessionID'"; > $result = mysql_query($query); > > } // end mysql_session_destroy() return true; -Rasmus -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] php sessions using mysql (or any db)
Cannot get code to work for session management on a database. Does anyone have tips. It appears that the data goes into the table, but I can't get it out. session_start just initializes the data in the value column.: SessionsTable definition --- CREATE TABLE `SessionsTable` ( `SID` varchar(32) NOT NULL default '', `expiration` int(11) NOT NULL default '0', `value` text NOT NULL, PRIMARY KEY (`SID`) ) TYPE=MyISAM mysql_sessions.inc: ". time(); $result = mysql_query($query); } // end mysql_session_select() //= // function: mysql_session_write() // purpose: This function writes the session data to the database. If that SID // already exists, then the existing data will be updated. //= function mysql_session_write($SID,$value) { GLOBAL $sess_db; GLOBAL $sess_table; GLOBAL $lifetime; $expiration = time() + $lifetime; $query = "INSERT INTO $sess_table VALUES('$SID', '$expiration', '$value')"; $result = mysql_query($query); if (! $result) : $query = "UPDATE $sess_table SET expiration = '$expiration', value = '$value' WHERE SID = '$SID' AND expiration >". time(); $result = mysql_query($query); endif; } // end mysql_session_write() //= // function: mysql_session_destroy() // purpose: deletes all session information having input SID (only one row) //= function mysql_session_destroy($sessionID) { GLOBAL $sess_table; $query = "DELETE FROM $sess_table WHERE SID = '$sessionID'"; $result = mysql_query($query); } // end mysql_session_destroy() //= // function: mysql_session_garbage_collect() // purpose: deletes all sessions that have expired. //= function mysql_session_garbage_collect($lifetime) { GLOBAL $sess_table; $query = "DELETE FROM $sess_table WHERE sess_expiration < ".time() - $lifetime; $result = mysql_query($query); return mysql_affected_rows($result); } // end mysql_session_garbage_collect() ?> End of mysql_sessions.inc --- 1-1.php - The first page --- Session Example #1 Welcome to a session-enabled page! The background color on the next page will be set to a stylish blue. Go to another session-enabled page. --- End of 1-1.php --- --- 1-2.php - To confirm that the session worked --- Session Example #1 \n", $_SESSION['bgcolor']); ?> -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php