phpbar.de logo

Mailinglisten-Archive

[php] PHP4 session storage mit MySQL

[php] PHP4 session storage mit MySQL

Holger Bahr hb_(at)_tsi-gmbh.de
Mon, 15 May 2000 20:53:51 +0200


<?php

/************************************************************************************
*  Hallo Liste,
*
*  jemand schon einmal php4 Session Storage mit einer
*  MySQL Datenbank gemacht ?
*  Ich habe da nämlich eine Anwendung die sich über mehrere
*  Server erstreckt und aus diesem Grund hätte ich gerne eine
*  zentrale Sessiondatenbank....
*  Wie auch immer - irgendwie möchte z.B. die Gargabe Collection
*  nicht so wie ich es gerne hätte.... Die Sessions leben quasi ewig.
*
*  Desweiteren würde ich gerne den ganzen Session Kram in
*  eine Klasse packen aber das will auch nicht so recht.
*
*  Ciao,
*            Holger (hb_(at)_waldorf-gmbh.de)
*
*
*   CREATE TABLE sessions (
*       session_id char(32) NOT NULL,
*       session_data char(255) NOT NULL,
*       timestamp timestamp(14),
*       UNIQUE session_id (session_id)
*    );
*
************************************************************************************/

// class my_weird_session_stuff {

	function connect() {
	   mysql_pconnect("YOUR_MYSQL_HOST_HERE","YOUR_USER_HERE","YOUR_PASSWORD_HERE");
	   mysql_select_db("YOUR_DATABASE_NAME_HERE");
	   return true;
	}

	function read_insert__select_inserted($key) {

	   $retval = mysql_query("SELECT session_data
	   			FROM sessions
				WHERE session_id = '$key'");
							
	   $result = mysql_fetch_row($retval);

		   if (!$result) {
		
	   		$actual_time_at_this_moment_on_this_server = time();
			mysql_query("INSERT INTO
				          sessions (session_id, timestamp)
				          VALUES ('$key', 
'$actual_time_at_this_moment_on_this_server')");		

			$retval = mysql_query("SELECT session_data
					           FROM sessions
					           WHERE session_id = '$key'");
					
			$result = mysql_fetch_row($retval);	
		   }	

	   return $result[0];
	}

	function close_after_destroy() {
	   print "FUNCTION CLOSE is/was EXECUTED<p>";
	   mysql_close(); // ????? where to put _close && / || _free_result
	   return true;
	}

	function update($key, $val) {
	   $actual_time_at_this_moment_on_this_server = time();
	   $retval = mysql_query("UPDATE sessions
	   			SET session_id = '$key', session_data = '$val', timestamp = 
'$actual_time_at_this_moment_on_this_server'
				WHERE session_id = '$key'");
	   return $retval;
	}

	function prepare_to_close ($key) {
	   $retval = mysql_query("DELETE FROM sessions WHERE session_id = '$key'");
	   return $retval;
	}

	function mysterious_garbager ($maxlifetime) {
	   $actual_time_at_this_moment_on_this_server = time();
	   $query = "DELETE FROM sessions WHERE timestamp < 
($actual_time_at_this_moment_on_this_server - $maxlifetime)";
	   mysql_query($query);
	   return true;
	}
	
	function my_weird_session_stuff() {           // Constructor
		session_set_save_handler("connect", "close_after_destroy", 
"read_insert__select_inserted", "update", "prepare_to_close", 
"mysterious_garbager");
	}

// } //end CLASS

// session_set_save_handler() = new my_weird_session_stuff();

  session_set_save_handler("connect", "close_after_destroy", 
"read_insert__select_inserted", "update", "prepare_to_close", 
"mysterious_garbager");
  session_start (); 			
  session_register("count");
?>
<body bgcolor = "#FAE7AD">
<?
  $count++;
  if ($count==6) {
	  	session_destroy();
		echo '<b>5</b> Times is Enough !<p><A HREF="sessioning.php">No ! Again 
!!</A>';
	} else {
?>
Hello,<br> our <A 
HREF="http://www.php.net/manual/ref.session.php3">Manual</A> has no deep 
explanation of the user session storage topic,<br>
but anyway, you have seen this page <b><?php echo $count; ?></b> times.<p>
To continue, <A HREF="sessioning.php?<?=SID?>">click here</A>
<?}?>



php::bar PHP Wiki   -   Listenarchive