codeIgniter 之 session fopen & touch 报错

最近使用 codeIgniter 开发项目,session 有报错。
在配置文件 config/config.php 中,对 session 进行如下配置:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] =  'tmp';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

第一次打开页面,tmp 目录下生成对应的 session 文件。再次,重新打开页面,却出现 Session_files_driver.php 文件报错。

报错提示,要么是 touch(),要么就是 fopen():

错误1

A PHP Error was encountered

Severity: Warning

Message: touch(): Unable to create file tmp\ci_sessionvjsqk6reon9agji530tbik959vgee8it because No such file or directory

Filename: drivers/Session_files_driver.php

Line Number: 250

Backtrace:

错误2

A PHP Error was encountered

Severity: Warning

Message: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (tmp)

Filename: Unknown

Line Number: 0

Backtrace:

打开 Session_files_driver.php 文件查看,对应的代码是“创建文件”与“打开文件”。

        public function read($session_id)
    {
        // This might seem weird, but PHP 5.6 introduces session_reset(),
        // which re-reads session data
        if ($this->_file_handle === NULL)
        {
            $this->_file_new = ! file_exists($this->_file_path.$session_id);

            ** if (($this->_file_handle = fopen($this->_file_path.$session_id, 'c+b')) === FALSE) **
            {
                log_message('error', "Session: Unable to open file '".$this->_file_path.$session_id."'.");
                return $this->_failure;
            }

            if (flock($this->_file_handle, LOCK_EX) === FALSE)
            {
                log_message('error', "Session: Unable to obtain lock for file '".$this->_file_path.$session_id."'.");
                fclose($this->_file_handle);
                $this->_file_handle = NULL;
                return $this->_failure;
            }
              .....
        }
        public function write($session_id, $session_data)
    {
        // If the two IDs don't match, we have a session_regenerate_id() call
        // and we need to close the old handle and open a new one
        if ($session_id !== $this->_session_id && ($this->close() === $this->_failure OR $this->read($session_id) === $this->_failure))
        {
            return $this->_failure;
        }

        if ( ! is_resource($this->_file_handle))
        {
            return $this->_failure;
        }
        elseif ($this->_fingerprint === md5($session_data))
        {
            ** return ( ! $this->_file_new && ! touch($this->_file_path.$session_id)) **
                ? $this->_failure
                : $this->_success;
        }

        if ( ! $this->_file_new)
        {
            ftruncate($this->_file_handle, 0);
            rewind($this->_file_handle);
        }

codeIgniter 的 session_file 使用流程是: open -> read -> write ->...
fopen() 报错时,跑过去 tmp 目录一看,明明就已经创建了对应的 session 文件。
那为何报错呢?那只能说明 fopen($this->_file_path) 中的 $this->_file_path 并不是指向前面的 tmp 目录中的 session 文件。
查看PHP 官方手册中 fopen() 函数说明,其中第三个参数说明内容如下:

use_include_path
The optional third use_include_path
 parameter can be set to '1' or **TRUE
** if you want to search for the file in the [include_path](http://php.net/manual/en/ini.core.php#ini.include-path), too.

这说明,在 use_include_path 为 false 情况下,那么 fopen($this->_file_path) 只会以 $this->_file_path 来搜索。
而 $this->_file_path 取自于 $config['sess_save_path'] = 'tmp'。对于 sys\libraries\Session\drivers\Session_files_driver.php 中的fopen(‘tmp’) 来说,这就是一个** 相对路径 ,只会取当前目录**下面的 tmp/session_file。

到这里,报错是没有错的,确实就是找不到该目录与文件。至于为何又会在 APPPATH . '/tmp' 下,生成了会话文件,这需要另外了解 codeIgniter 的会话产生机制。

Session_files_driver.php 文件执行过程:

initialize - 初始化
---2-open - 
-----3--read

》输出页面内容 《

----4---write
-----3--read

解决办法就是:将 $config['sess_save_path'] 设置为绝对路径,如:** $config['sess_save_path'] = APPPATH . 'tmp';**

参考文章链接

你可能感兴趣的:(codeIgniter 之 session fopen & touch 报错)