用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1656|回复: 1
收起左侧

[HELP] PHP 中加入session_set_save_handler之后session就失效

[复制链接]
发表于 2016-10-5 21:31:59 | 显示全部楼层 |阅读模式
我在CI3.0下面发现多次请求同一个页面,session居然会被清空,然后做了以下的试验,发现症状所在。如果在session start 之前调用了session_set_save_handler,那么无论我刷新多少次网页,我下面的代码都会输出'no foo data',也就是说session里面的值一直没有保存,如果去除session_set_save_handler,那么刷新网页就会发现输出'have the foo data',不知道怎么回事,请大神赐教一下!搞了半天了。。。
PHP复制代码
<?php
/*
Session open (called by session_start( ))
Session close (called at page end)
Session read (called after session_start( ) )
Session write (called when session data is to be written)
Session destroy (called by session_destroy( ) )
Session garbage collect (called randomly)
*/

function sess_open($sess_path, $sess_name) {
    //print "Session opened.\n";
    //print "Sess_path: $sess_path\n";
    //print "Sess_name: $sess_name\n\n";
    return true;
}
 
function sess_close( ) {
    //print "Session closed.\n";
    return true;
}
 
function sess_read($sess_id) {
    //print "Session read.\n";
    //print "Sess_ID: $sess_id\n";
    return '';
}
 
function sess_write($sess_id, $data) {
    //print "Session value written.\n";
    //print "Sess_ID: $sess_id\n";
    //print "Data: $data\n\n";
    return true;
}
 
function sess_destroy($sess_id) {
    //print "Session destroy called.\n";
    return true;
}
 
function sess_gc($sess_maxlifetime) {
    //print "Session garbage collection called.\n";
    //print "Sess_maxlifetime: $sess_maxlifetime\n";
    return true;
}
session_set_save_handler('sess_open','sess_close','sess_read','sess_write','sess_destroy','sess_gc');
session_register_shutdown('session_write_close');
session_start();
 
if(isset($_SESSION['foo']))
$_SESSION['foo'] = "have the foo data";
else $_SESSION['foo'] = "no foo data";
 
echo  $_SESSION['foo'];
?>
复制代码

发表于 2016-12-29 18:17:56 | 显示全部楼层
你把session_set_save_handler()放到session_start()前时,你没有给session存储地址,所以找不到。放在后面是因为php的配置文件中有默认的存储地址,好像是session.save_path,所以能找到。

本版积分规则