php session 自定义的设置测试
<?php
// ini_set(‘session.save_handler‘,
‘user‘);
// 注意 set_session_save_handler() 一定要在
session_start()前执行
// session.gc_probability
=1 //session
回收机制的概率分子
// session.gc_divisor =
1000 // session 回收机制的概率分母
//
session.gc_maxlifetime = 1440 // session 有效期
// 当开启
session 时,如 session_start(),
// 执行顺序为 sess_open() ->
sess_read()->sess_write() -> sess_close() ->sess_destroy() ->
sess_gc()
ini_set("session.save_handler", "user"); //
自定义保存方式
function sess_open($sess_path, $sess_name)
{
echo "session 路径:" . $sess_path . "<br />";
echo "session名称:" . $sess_name . "<br />";
return true;
}
function sess_close()
{
echo "session 关闭<br />";
return
true;
}
function sess_read($sess_id)
{
echo
"读取session id :" . $sess_id . "<br />";
return
true;
}
function sess_write($sess_id,
$data)
{
echo "写入数据 session id " . $sess_id . "<br
/>";
echo "session 数据 " . $data . "<br
/>";
return true;
}
function
sess_destroy($sess_id)
{
echo "销毁 session id " .
$sess_id . "<br />";
return
true;
}
function sess_gc($sess_maxlifetime)
{
echo "session 回时机制,每隔时间为 " . $sess_maxlifetime;
return true;
}
session_set_save_handler("sess_open",
"sess_close", "sess_read", "sess_write", "sess_destroy",
"sess_gc");
session_start();
$_SESSION[‘lin‘] = ‘lin3615‘;
echo ‘hi,
world ‘;
$_SESSION[‘long‘] = ‘good luck for you‘;
// 开启 cookie 时,默认时会以
session 名称 PHPSESSID 为了cookie 为记录
// 如 $_COOKIE[‘PHPSESSID‘] = session_id;
值即为session_id值
print_r($_COOKIE);
?>
// 以下为保存在数据库中
<?php
数据库为 test
数据表结构
CREATE TABLE `ws_sessions` (
`session_id` varchar(255) NOT
NULL default ‘‘,
`session_expires` int(11) unsigned NOT NULL default
‘0‘,
`session_data` text,
PRIMARY KEY
(`session_id`)
)
class session
{
public
$lifeTime;
public $dbHandler;
function open($savePath, $sessName)
{
$this->lifeTime =
get_cfg_var("session.gc_maxlifetime");
$dbHandler = mysql_connect(‘localhost‘, ‘root‘,
‘rootlin3615‘);
$dbSel =
mysql_select_db(‘test‘, $dbHandler);
if(!$dbHandler || !$dbSel)
return false;
$this->dbHandler = $dbHandler;
return true;
}
function close()
{
$this->gc(ini_get("session.gc_maxlifetime"));
return
mysql_close($this->dbHandler);
}
function read($sessID)
{
$res = mysql_query("select session_data as d from
ws_sessions
where session_id=‘{$sessID}‘
and session_expires > " . time(),
$this->dbHandler);
if($row =
mysql_fetch_assoc($res))
return $row[‘d‘];
return
‘‘;
}
function write($sessID,
$sessData)
{
$newExp = time()+$this->lifeTime;
$res = mysql_query("select * from ws_sessions
where
session_id=‘{$sessID}‘", $this->dbHandler);
if(mysql_num_rows($res))
{
mysql_query("update ws_sessions set session_data =
‘{$sessData}‘",$this->dbHandler);
if(mysql_affected_rows($this->dbHandler))
return
true;
}else
{
mysql_query("insert into ws_sessions (
session_id,
session_expires, session_data)
values(‘{$sessID}‘, ‘{$newExp}‘, ‘{$sessData}‘)",
$this->dbHandler);
if(mysql_affected_rows($this->dbHandler))
return
true;
}
return false;
}
function destroy($sessID)
{
mysql_query("delete from
ws_sessions where session_id = ‘{$sessID}‘",
$this->dbHandler);
if(mysql_affacted_rows($this->dbHandle))
return true;
return false;
}
function gc($sessMaxLifeTime)
{
mysql_query("delete from
ws_sessions where session_expires < " . time(),
$this->dbHandler);
return
mysql_affected_rows($this->dbHandler);
}
}
ini_set("seession.save_handler", ‘user‘);
$session = new
session();
session_set_save_handler(array($session,"open"),
array($session,"close"),
array($session,"read"),
array($session,
"write"),
array($session, "destroy"),
array($session,
"gc"));
session_start();
//把上面整理为一个文件,然后包含进来,就可以像把 session保存在文件的方式使用了
// 如下
$_SESSION[‘lin‘] = ‘hi,lin3615‘;
print_r($_SESSION);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。