//XX.PHP自定義了session的數據庫路徑,當某個頁面需要使用//session時,可以include這個部分,使用方法為:
include "XX.php";//XX.PHP
session_start();
//以下就可以正常使用session了
?>
/******************************************************/
XX.PHP 內容:
/*****************************************************/
$sess_dbh="";
$sess_maxlifetime=get_cfg_var("session.gc_maxlifetime");
function sess_open($save_path, $session_name) {
global $hostname, $dbusername, $dbpassWord, $dbname, $sess_dbh;
//$sess_dbh=MySQL_pconnect($hostname,$dbusername,$dbpassWord) or dIE("不能連接數據庫!");
$sess_dbh=MySQL_pconnect('localhost','test','test') or dIE("不能連接數據庫!");
// MySQL_select_db("$dbname") or dIE("不能選擇數據庫!");
MySQL_select_db('test') or dIE("不能選擇數據庫!");
return(true);
}
function sess_close() {
//MySQL_close();
return(true);
}
function sess_read($sid) {
global $sess_dbh;
$result = MySQL_query("select data from sesslib where sid='$sid'", $sess_dbh);
$n=MySQL_num_rows($result);
if($n==0) {
return("");
}
else {
$sess_data=MySQL_result($result,0);
return($sess_data);
}
}
function sess_write($sid, $sess_data) {
global $sess_dbh;
if(!empty($sess_data)){
$r=MySQL_query("insert into sesslib set sid='$sid',data='$sess_data',time=now()", $sess_dbh);
if(!$r) { // insertion failed, means the session is already there, update it
$r=MySQL_query("update sesslib set sid='$sid', data='$sess_data', time=now() where sid='$sid'",$sess_dbh);
}
return $r;
}}
function sess_destroy($sid) {
global $sess_dbh;
$r=MySQL_query("delete from sesslib where sid='$sid'", $sess_dbh);
return($r);
}
function sess_gc($maxlifetime) {
global $sess_dbh, $sess_maxlifetime;