search
Homephp教程PHP开发ThinkPHP custom Redis implementation method to handle SESSION

The example in this article describes the implementation method of ThinkPHP custom Redis processing SESSION. Share it with everyone for your reference, the details are as follows:

In daily life, we use session to save user login information. Commonly used session saving methods include: file saving (default), database saving, Redis saving, memcached, etc. Here we mainly record the usage of using Redis to save the session when using ThinkPHP to process the session.

1. Define in the configuration item:

'SESSION_TYPE' => 'Redis', //session保存类型
'SESSION_PREFIX' => 'sess_', //session前缀
'REDIS_HOST' => '127.0.0.1' //REDIS服务器地址
'REDIS_PORT' => 6379, //REDIS连接端口号
'SESSION_EXPIRE' => 3600, //SESSION过期时间

You can find the method of defining session in the ThinkPHP/Common/functions.php file, and read the judgment of the session driver at around line 1179. If we define the configuration item SESSION_TYPE, a new Redis object will be created and the session storage function session_set_save_handler() will be called.

2. Create a new Redis.class.php file in the ThinkPHPLibraryThinkSessionDriver directory

The content of the file is as follows:

<?php
namespace Think\Session\Driver;
class Redis {
//  Redis连接对象
  private $redis;
//  Session过期时间
  private $expire;
  /**
   * 打开方法
   * @param type $path
   * @param type $name
   * @return type
   */
  public function open($path, $name) {
  $this->expire = C(&#39;SESSION_EXPIRE&#39;) ? C(&#39;SESSION_EXPIRE&#39;) : ini_get(&#39;session.gc_maxLifetime&#39;);
  $this->redis = new Redis();
  return $this->redis->connect(C(&#39;REDIS_HOST&#39;), C(&#39;REDIS_PORT&#39;));
  }
  /**
   * 关闭
   * @return type
   */
  public function close() {
  return $this->redis->close();
  }
  /**
   * 读取
   * @param string $id
   * @return type
   */
  public function read($id) {
  $id = C(&#39;SESSION_PREFIX&#39;) . $id;
  $data = $this->redis->get($id);
  return $data ? $data : &#39;&#39;;
  }
  /**
   * 写入
   * @param string $id
   * @param type $data
   * @return type
   */
  public function write($id, $data) {
  $id = C(&#39;SESSION_PREFIX&#39;) . $id;
  return $this->redis->set($id, $data, $this->expire);
  }
  /**
   * 销毁
   * @param string $id
   */
  public function destroy($id) {
  $id = C(&#39;SESSION_PREFIX&#39;) . $id;
  $this->redis->delete($id);
  }
  /**
   * 垃圾回收
   * @param type $maxLifeTime
   * @return boolean
   */
  public function gc($maxLifeTime) {
  return true;
  }
}

This completes the session processing by Redis.

The method of memcached is almost the same as Redis!

I hope this article will help you design PHP programs based on the ThinkPHP framework.

For more articles related to ThinkPHP’s custom Redis processing SESSION implementation method, please pay attention to 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment