Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- mysql_connect('localhost','user','pass');
- mysql_select_db('mydb');
- function on_session_start($save_path, $session_name)
- {
- error_log($session_name . ' '. session_id());
- }
- function on_session_end()
- {
- // Nothing needs to be done in this function
- // since we used persistent connection.
- }
- function on_session_read($key)
- {
- error_log($key);
- $stmt = 'SELECT session_data FROM sessions ';
- $stmt .= "WHERE session_id='$key' ";
- $stmt .= 'AND unix_timestamp(session_expiration) > unix_timestamp(date_add(now(),interval 1 hour))';
- $sth = mysql_query($stmt);
- if($sth)
- {
- $row = mysql_fetch_array($sth);
- return($row['session_data']);
- }
- else
- {
- return $sth;
- }
- }
- function on_session_write($key, $val)
- {
- error_log("$key = $value");
- $val = addslashes($val);
- $insert_stmt = "INSERT INTO sessions values('$key', ";
- $insert_stmt .= "'$val',unix_timestamp(date_add(now(), interval 1 hour)))";
- $update_stmt = "UPDATE sessions SET session_data ='$val', ";
- $update_stmt .= "session_expiration = unix_timestamp(date_add(now(), interval 1 hour))";
- $update_stmt .= "WHERE session_id ='$key '";
- // First we try to insert, if that doesn't succeed, it means
- // session is already in the table and we try to update
- mysql_query($insert_stmt);
- $err = mysql_error();
- if ($err != 0)
- {
- error_log( mysql_error());
- mysql_query($update_stmt);
- }
- }
- function on_session_destroy($key) {
- mysql_query("DELETE FROM sessions WHERE session_id = '$key'");
- }
- function on_session_gc($max_lifetime)
- {
- mysql_query("DELETE FROM sessions WHERE unix_timestamp(session_expiration) < unix_timestamp(now())");
- }
- // Set the save handlers
- // This will make the session functions/variables use our functions instead.
- session_set_save_handler("on_session_start", "on_session_end",
- "on_session_read", "on_session_write",
- "on_session_destroy", "on_session_gc");
- // Carry on as if you were normal
- session_start();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement