Re: PHP Session Management
| From: | CDitty | Date: | Sun, 23 Mar 2003 15:20:45 +0000 |
| Subject: | Re: PHP Session Management | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-140756@lists.php.net to get a copy of this message | ||
Got it fixed. Turns out there are typos in the book
Thanks Jason for the help.
Chris
At 08:50 AM 3/23/2003, CDitty wrote:
Hello all. I am trying to figure out some session management that is in the O'reilly book - Web Database Applications. I am getting the following error - Error 1045 : Access denied for user: 'userid@localhost' (Using password: YES) Normally, I would think that this is because the userid and/or password for the database is incorrect. However, they are not. I have verified that the userid and password are correct and the database server is active. Anyone have any ideas? Here is the code.... sessions.php --------------------- <? // Database connection information $hostName = "localhost"; $databaseName = "development"; $username = "userid"; $password = "password"; // Error handling function showerror() {die("Error " . mysql_errno() . " : " . mysql_error());} // Returns current time as a number // Used for recording the last session access function getMicroTime() {// microtime() returns the number of seconds // since 0:00:00 January 1, 1970 GMT as a // microsecond part and a second part. // eg: 0.08344800 1000952237 // Convert the two parts into an array $mtime = explode(" ", microtime());// Return the addition of the two parts return($mtime[1] + $mtime[0]);} // The database connection $connection; // The global variable that holds the table name $session_table; function sessionOpen($database_name, $table_name) {// Save the database name in a global variable global $connection; global $hostName; global $username; global $password;if(!($connection = @mysql_connect($hostName, $username, $password))){ showerror(); }if(!mysql_select_db($database_name, $connection)){ showerror(); }// Save the table name in a global variable global $session_table; $session_table = $table_name;return true;} // This function is called whenever a session_start() // call is made and reads the session variables // Returns "" when a session is not found // (serialized)string - session exists function sessionRead($sess_id) {// Access the DB connection global $connection;// Access the global variable that holds the name // of the table that holds the session variables global $session_table;// Formulate a query to find the session // identified by $sess_id $search_query = "select * from $session_table where session_id = '$sess_id'";// Execute the query if(!($result = @mysql_query($search_query, $connection))){ showerror(); }if(mydql_num_rows($result) == 0){ return ""; }else{ // Found a session - retun the seialized string $row = mysql_fetch_array($result); return $row["session_variable"]; }} // This function is called when a session is initialized // with a session_start() call, when variables are // registered or unregistered, and when session variables // are modified. Returns true on success. function sessionWrite($sess_id, $val) {global $connection; global $session_table;$time_stamp = getMicroTime();$search_query = "select session_id from $session_table where session_id = '$sess_id'";// Execute query if(!($result = @mysql_query($search_query, $connection))){ showerror(); }if(mysql_num_rows($result) == 0){ // No session found, insert a new one $insert_query = "insert into $session_table (session_id, session_variable, last_accessed) values ('$sess_id, '$val', $time_stamp)";if(!mysql_query($insert_query, $connection)){ showerror(); } }else{ // Existing session found - Update it $update_query = "update $session_table set session_variable = '$val', last_accessed = $time_stamp where session_id = '$sess_id'";if(!mysql_query($update_query, $connection)){ showerror(); } } return true;} // This funstion is executed on shutdown of the session // Always returns true function sessionClose($sess_id) {return true;} // This is called whenever the session_destroy() // funstion call is made. Returns true is the session // has successfully been deleted. function sessionDestroy($sess_id) {global $connection; global $session_table;$delete_query = "delete from session_table where session_id = '$sess_id'";if(!($result = @mysql_query($delete_query, $connection))){ showerror(); } return true;} // This function is called on a session's start up with // the probability specified in session.gc_probability. // Performs garbage collection by removing all sessions // that haven't been updated in the last $max_lifetime // seconds as set in session.gc_maxlifetime. // returns true if the delete query succeeded. function sessionGC($max_lifetime) {global $connection; global $session_table;$time_stamp = getMicroTime();$delete_query = "delete from $session_table where last_accessed < ($time_stamp - $max_lifetime)";if(!($result = @mysql_query($delete_query, $connection))){ showerror(); }return true;} // Call to register user callback functions session_set_save_handler("sessionOpen", "sessionClose", "sessionRead", "sessionWrite", "sessionDestroy", "sessionGC"); ?> Here is the calling code. sessTest.php --------------------- <? // include the sessions handlers include("sessions.php"); // initialize a session. This call either creates a new session // or re-establishes an existing one. session_start(); // if this is a new session, then the variable $count is not registered if(!session_is_registered("count")) {session_register("count"); session_register("start");$count = 0; $start = time();}else{$count++;} $sessionId = session_id(); ?> <p>This page points at a session (<?=$sessionId ?>)<br> count = <?=$count ?>.<br> start = <?=$start ?>.<br> <p>This session has lasted <? $duration = time() - $start; echo "$duration"; ?> seconds. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php