I am having no success as using MySQL as a storage module for session ids.
I am running php 4.0.1pl2 and am able to get sessions to work when the session.save_handler is set to files and stores session IDs in /tmp. But when I set it to user and use the presupplied storage module by Tobias from his php 4 application book my code chokes on session_start.
I have MySQL all set up and working for other projects.
session.auto_start is also set to off in php.ini (I don't know if it should be on).
<?php
/****************************************************************************
Web Application Development with PHP
by
Tobias Ratschiller and Till Gerken
Copyright (c) 2000, Tobias Ratschiller and Till Gerken
*
- *
- $Title: MySQL storage module for PHP 4.0's session library $ *
- $Chapter: Web Application Concepts $ *
- $Executable: false $ *
- *
- $Description: *
- This is user-level storage module for PHP 4.0's session library. *
- These functions can be registered by calling session_set_save_handler(). $*
****************************************************************************/
/
See the bottom of this file for an example how these functions.
You'll also need to set an appopiate MySQL table with this schema:
phpMyAdmin MySQL-Dump
#
Host: localhost Database : book
--------------------------------------------------------
#
# Table structure for table 'sessions'
#
CREATE TABLE sessions (
id varchar(50) NOT NULL,
data mediumtext NOT NULL,
t_stamp timestamp(14),
PRIMARY KEY (id),
KEY t_stamp (t_stamp)
);
/
$sess_mysql = array();
$sess_mysql["open_connection"] = true; // Establish a MySQL connection on session startup?
$sess_mysql["hostname"] = "localhost"; // MySQL hostname
$sess_mysql["user"] = "root"; // MySQL username
$sess_mysql["password"] = ""; // MySQL password
$sess_mysql["db"] = "book"; // Database where to store the sessions
$sess_mysql["table"] = "sessions"; // Table where to store the sessions
function sess_mysql_open($save_path, $sess_name)
{
global $sess_mysql;
// Establish a MySQL connection, if $sess_mysql["open_connection"] is true
if ($sess_mysql["open_connection"])
{
$link = mysql_pconnect($sess_mysql["hostname"], $sess_mysql["user"], $sess_mysql["password"]) or die(mysql_error());
}
return(true);
}
function sess_mysql_read($sess_id)
{
global $sess_mysql;
// Select the data belonging to session $sess_id from the MySQL session table
$result = mysql_db_query($sess_mysql["db"], "SELECT data FROM ".$sess_mysql["table"]." WHERE id = '$sess_id'") or die(mysql_error());
// Return an empty string if no data was found for this session
if(mysql_num_rows($result) == 0)
{
return("");
}
// Session data was found, so fetch and return it
$row = mysql_fetch_array($result);
mysql_free_result($result);
return($row["data"]);
}
function sess_mysql_write($sess_id, $val)
{
global $sess_mysql;
// Write the serialized session data ($val) to the MySQL ession table
$result = mysql_db_query($sess_mysql["db"], "REPLACE INTO ".$sess_mysql["table"]." VALUES ('$sess_id', '$val', null)") or die(mysql_error());
return(true);
}
function sess_mysql_destroy($sess_id)
{
global $sess_mysql;
// Delete from the MySQL table all data for the session $sess_id
$result = mysql_db_query($sess_mysql["db"], "DELETE FROM ".$sess_mysql["table"]." WHERE id = '$sess_id'") or die(mysql_error());
return(true);
}
function sess_mysql_gc($max_lifetime)
{
global $sess_mysql;
// Old values are values with a Unix less than now - $max_lifetime
$old = time() - $max_lifetime;
// Delete old values from the MySQL session table
$result = mysql_db_query($sess_mysql["db"], "DELETE FROM ".$sess_mysql["table"]." WHERE UNIX_TIMESTAMP(t_stamp) < $old") or die(mysql_error());
return(true);
}
/
Basic Example: Registering the above functions with session_set_save_handler()
$foo = 10;
session_set_save_handler("sess_mysql_open", "", "sess_mysql_read", "sess_mysql_write", "sess_mysql_destroy", "sess_mysql_gc");
session_start();
session_register("foo");
echo "foo: $foo";
$foo++;
*/
/ $Id: MySQL_Session_Module.php,v 1.2 2000/06/15 19:51:10 tobias Exp $ /
?>