<?php
/*
* usage as:
*
* date_default_timezone_set("PRC");
* set_session_mysql("localhost:3306", "root", "", "db");
* session_start();
*
* #session table
*
CREATE TABLE `session` (
`id` varchar(100) NOT NULL DEFAULT '',
`value` mediumblob NOT NULL,
`ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
*/
$session_mysql_conn = null;
$session_mysql_host = null;
$session_mysql_user = null;
$session_mysql_pwd = null;
$session_mysql_db = null;
$session_mysql_flag = false;
function open($save_path, $session_name)
{
global $session_mysql_host;
global $session_mysql_user;
global $session_mysql_pwd;
global $session_mysql_db;
if(null == $session_mysql_host
|| null == $session_mysql_user
|| null == $session_mysql_db)
return false;
global $session_mysql_conn;
$session_mysql_conn = mysql_connect($session_mysql_host, $session_mysql_user, $session_mysql_pwd);
if(false == $session_mysql_conn)
return (false);
if(false == mysql_select_db($session_mysql_db, $session_mysql_conn))
{
mysql_close($session_mysql_conn);
$session_mysql_conn = null;
return (false);
}
return(true);
}
function close()
{
return(true);
}
function read($id)
{
global $session_mysql_conn;
if(null == $session_mysql_conn)
return null;
$select_sql = "select value from session where id = '$id'";
$result = mysql_query($select_sql, $session_mysql_conn);
if(false == $result)
return null;
if(0 == mysql_num_rows($result))
return null;
$row = mysql_fetch_row($result);
if(empty($row))
return null;
$res = $row[0];
mysql_free_result($result);
//echo "session_mysql_read:$id = $res </br>";
return $res;
}
function write($id, $sess_data)
{
//echo "session_mysql_write $id = $sess_data </br>";
global $session_mysql_conn;
global $session_mysql_flag;
if(null == $session_mysql_conn)
return false;
$value = mysql_real_escape_string($sess_data);
$write_sql="update session set value = '$value' where id = '$id'";
if(false == $session_mysql_flag)
{
$select_sql = "select count(id) from session where id = '$id'";
$result = mysql_query($select_sql, $session_mysql_conn);
if(false == $result)
return false;
$row = mysql_fetch_row($result);
if(empty($row))
return false;
if($row[0] == 0)
{
$write_sql="insert into session (id,value) values ('$id', '$value')";
}
else
{
$session_mysql_flag = true;
}
}
return mysql_query($write_sql, $session_mysql_conn);
}
function destroy($id)
{
global $session_mysql_conn;
if(null == $session_mysql_conn)
return false;
mysql_close($session_mysql_conn);
$session_mysql_conn = null;
}
function gc($maxlifetime)
{
global $session_mysql_conn;
if(null == $session_mysql_conn)
return false;
$t = time() - $maxlifetime;
$d = date("Y-m-d h:i:s",$t);
$delete_sql = "delete from session where ctime < '$d'";
return mysql_query($delete_sql, $session_mysql_conn);
}
function set_session_mysql($s_mysql_host, $s_mysql_user, $s_mysql_pwd, $s_mysql_db)
{
global $session_mysql_host;
global $session_mysql_user;
global $session_mysql_pwd;
global $session_mysql_db;
$session_mysql_host = $s_mysql_host;
$session_mysql_user = $s_mysql_user;
$session_mysql_pwd = $s_mysql_pwd;
$session_mysql_db = $s_mysql_db;
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
}
?>
session_set_save_handler实例
最新推荐文章于 2021-03-22 04:51:57 发布