codeIgniter 之 session fopen & touch 報錯

weixin_33797791發表於2017-03-24

最近使用 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';**

參考文章連結

相關文章