SESSION 使用出错
本帖最后由 v阿杰 于 2015-4-19 14:27 编辑前提是我开启了自动加载Session类
A PHP Error was encountered
Severity: Warning
Message: session_set_save_handler() expects parameter 1 to be SessionHandler, object given
Filename: Session/Session.php
Line Number: 107
Backtrace:
File: E:\www\imix\index.php
Line: 292
Function: require_once
:(
把session.php的107行注释了
if (is_php('5.4'))
{
#session_set_save_handler($class, TRUE);
}
else
{
session_set_save_handler(
array($class, 'open'),
array($class, 'close'),
array($class, 'read'),
array($class, 'write'),
array($class, 'destroy'),
array($class, 'gc')
);
register_shutdown_function('session_write_close');
}
參考CodeIgnter 3.0.0 system/libraries/Session/session.php源碼
$class = new $class($this->_config);
if ($class instanceof SessionHandlerInterface)
{
if (is_php('5.4'))
{
session_set_save_handler($class, TRUE);
}
else
{
session_set_save_handler(
array($class, 'open'),
array($class, 'close'),
array($class, 'read'),
array($class, 'write'),
array($class, 'destroy'),
array($class, 'gc')
);
session_set_save_handler($class, TRUE);句報錯!
執行前的檢測不單是PHP是5.4或更高,還有必須"$class instanceof SessionHandlerInterface"為真。
再参看system/libraries/Session/drivers/Session_files_driver.php中
class CI_Session_files_driver extends CI_Session_driver implements SessionHandlerInterface {
var_dump()顯示$class是CI_Session_files_driver,完全正確!即是說,$class應該是SessionHandler!
但執行session_set_save_handler時卻認為$class並非SessionHandler!相當奇怪!
若系統中PHP的session extension無出錯,可能是session_set_save_handler使用$class內容時出錯,故此認為$class並非SessionHandler。
参看var_dump()的$class內涵,"save_path"的確相當可疑,3.0手冊指明要求絕對路徑,但var_dump()出來是相對路徑。
PHP 5.4及5.5自動加載session我剛試過,完全無問題。若非這些問題,估計是其它環境設定引致。
至於為何自動加載才見到問題,可能非自動加載時亦有,但一閃而過。可打開log功能看看是否如此。 阅读手册也没找到原因,自动加载session就是出错。 老樣子,請貼
1. Session 配置
2. 使用時的呼叫方式 什么都不修改!只是在自动加载中添加session,就报错了:( v阿杰 发表于 2015-4-20 09:56
什么都不修改!只是在自动加载中添加session,就报错了
$config['sess_driver'] = 'files';
$config['sess_save_path'] = ''; //請設置絕對路徑 $config['sess_save_path'] = FCPATH.'public/sess_save_path/';
可是还是一样的错误!!! 绝对和相对都可以 看到sess_save_path 这个目录下生成文件了 正在研究这个函数的参数: 以为版本大于5.4 所以:运行了session_set_save_handler($class, TRUE);
就是这个$class的错误:
object(CI_Session_files_driver)#17 (8) {
["_save_path":protected]=>
NULL
["_file_handle":protected]=>
NULL
["_file_path":protected]=>
NULL
["_file_new":protected]=>
NULL
["_config":protected]=>
&array(8) {
["cookie_lifetime"]=>
int(7200)
["cookie_name"]=>
string(10) "ci_session"
["cookie_path"]=>
string(1) "/"
["cookie_domain"]=>
string(0) ""
["cookie_secure"]=>
bool(false)
["expiration"]=>
int(7200)
["match_ip"]=>
bool(false)
["save_path"]=>
string(23) "./public/sess_save_path"
}
["_fingerprint":protected]=>
NULL
["_lock":protected]=>
bool(false)
["_session_id":protected]=>
NULL
}
我觉得你配置还是错误 存储路径还是错误应该考虑下存储的目录是否存在 是否具有可读可写的权限 我的意思是你按closer说的配置完存储的路径后还是报错的话,应该考虑下 是否是目录不存在 以及 权限问题