Home  >  Article  >  Backend Development  >  What to do if php session writing fails

What to do if php session writing fails

藏色散人
藏色散人Original
2021-07-09 10:16:023118browse

php session writing failed because the session was mistakenly stored in memcache. The solution is to store the session in redis, and then modify the php configuration file.

What to do if php session writing fails

The operating environment of this article: windows10 system, PHP7.1 version, DELL G3 computer

Specific questions:

What should I do if php session fails to write?

php keeps reporting session writing failure

Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: tcp://127.0.0.1:6379) in /var/www/html/php/libs/util/Session.php on line 43

What to do if php session writing fails

Problem description

I keep reporting this problem when writing my code into session. The environment is Windows 10 Ubuntu subsystem, the location where the session is written is memcache port 6379, and the extensions are enabled. Can anyone tell me what the problem is?

The environmental background of the problem and what methods you have tried

Related code

class Session {
  // single ton
  private static $session_handler_;
  public static function Init() {
    if (!isset(self::$session_handler_))
      self::$session_handler_ = new Session();
  }
  // 阻止用户复制对象实例
  public function __clone() {
    trigger_error('Clone Session is not allowed.', E_USER_ERROR);
  }
  private function __construct() {
    session_set_save_handler(array($this, "Open"),
                             array($this, "Close"),
                             array($this, "Read"),
                             array($this, "Write"),
                             array($this, "Destroy"),
                             array($this, "Gc"));
    // for web user
    $session_id = Cookie::Get(SESSIONID);
    /* length of session id
     128-bit digest (MD5)
     4 bits/char: 32 char SID
     5 bits/char: 26 char SID
     6 bits/char: 22 char SID
     160-bit digest (SHA-1)
     4 bits/char: 40 char SID
     5 bits/char: 32 char SID
     6 bits/char: 27 char SID
    */
    if (!empty($session_id) && 26 == strlen($session_id))
      session_id($session_id);
    session_start();
  }
  public function __destruct() {
    session_write_close();
  }
  public function Open($save_path, $session_name) {
    return true;
  }
  public function Close() {
    return true;
  }
  public function Read($session_id) {
    $key = SESSION_PREFIX . $session_id;
    $memcached_client_ = SessionMemCachedClient::GetInstance();
    return (string)$memcached_client_->get($key);
  }
  public function Write($session_id, $data) {
    $key = SESSION_PREFIX . $session_id;
    $memcached_client_ = SessionMemCachedClient::GetInstance();
    if ($data)
      return $memcached_client_->set($key, $data, SESSION_EXPIRE_TIME);
    return true;
  }
  public function Destroy($session_id) {
    $key = SESSION_PREFIX . $session_id;
    $memcached_client_ = SessionMemCachedClient::GetInstance();
    return $memcached_client_->delete($key);
  }
  public function Gc($maxlifetime) {
    return true;
  }
}
Session::Init();

What is the result you expect? What is the actual error message you see?

Solution:

The problem has been found. We now store the session in memcache. Memcache uses port 6379, but before I stored the session in memcache. In redis, I changed the php configuration file. I forgot about this, so I always thought there was something wrong with the code.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What to do if php session writing fails. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn