How PHP uses Redis alternative files to store session

Source: Internet
Author: User
Tags session id redis server
This article mainly introduces the use of Redis alternative file storage session method, combined with the case of a detailed analysis of the session of the basic operating methods and the use of Redis storage session related skills, the need for friends can refer to the next

The example in this article describes how PHP uses Redis alternative files to store the session. Share to everyone for your reference, as follows:

PHP uses the file storage session by default, which is very inefficient if the concurrency is large. Redis's support for high concurrency is very good, so you can store the session using Redis alternative files.

Here, we introduce the session_set_save_handler function of PHP and how to use it. This function defines a user-level session save function (such as open, close, write, etc.).

The prototype is as follows:

bool session_set_save_hanler(callback open,callback close,callback read,callback write,callback destory,callback gc)

The functions of Session_set_save_handler function are as follows table


parameter description
open Call this function when session is open. Receive two parameters, the first parameter is the path to keep the session, the second argument is the name of the session
close This function is called when the session operation is complete. No parameters are received.
read session_start was triggered before
write
destroy when called session_destroy The Destroy function is triggered when function. Only one parameter session ID
Gc Triggered when PHP executes the session garbage collection mechanism


Before using this function, set the Session.save_handler option of the php.ini configuration file to user, otherwise session_set_save_handle will not take effect.

Write a session management class sessionmanager.php with the following code:

<?phpclass sessionmanager{private $redis; private $sessionSavePath; private $sessionName; private $ Sessionexpiretime=30;//redis,session Expiration Time is 30s public function __construct () {$this->redis = new Redis ();// Create a Phpredis instance $this->redis->connect (' 127.0.0.1 ', 6379);//Connect Redis $this->redis->auth ("107lab");//Authorize $ retval = Session_set_save_handler (Array ($this, "open"), Array ($this, "close"), Array ($this, "read"), Array ($this, " Write "), Array ($this," destroy "), Array ($this," GC ")); Session_Start (); Public function open ($path, $name) {return true;} public function Close () {return true;} public function Read ($id) {$va Lue = $this->redis->get ($id);//Gets the specified record in Redis if ($value) {return $value;} else{return ';}} Public function Write ($id, $data) {if ($this->redis->set ($id, $data)) {//session ID is key, storage $this->redis-> Expire ($id, $this->sessionexpiretime);//Set the expiration time of data in Redis, that is, the expiration time of the session return true; } return false; The Public function destroy ($id) {if ($this->redis->delete ($id)) {//delete specified record in Redis return true;} return false; The Public Function gc ($MAXLIFETIME) {return true;} public Function __destruct () {Session_write_close ()}}

The SessionManager constructor is primarily used to connect to a Redis server, use the Session_set_save_handler function to set the session callback function, and invoke the Session_Start function to open the session function. Because the open, close, and GC callback functions in this example do not work very large, they return true directly.

In the write callback function, with session ID as key, the session data is stored as value to the Redis server, setting the session expiration time is 30 seconds. In the read callback, the session ID is used as the key to read the data from the Redis server and return this data. When the destroy callback function is heavy, the session ID is used as key to remove the corresponding session data from the Redis server.

When used, simply include the SessionManager class and instantiate a SessionManager object. Create a session_set.php file below. Input code

<?php include (' sessionmanager.php '); New SessionManager (); $_session[' username '] = ' Captain ';

Then create a session_get.php file and enter the following code:

<?php include (' sessionmanager.php '); New SessionManager (); echo $_session[' username '];

When testing, first access the session_set.php, and then access the session_get.php, the output results are as follows:

Then look at the Redis database as shown below

127.0.0.1:6379> keys *) "oe94eic337slnjv1bvlreoa574" 127.0.0.1:6379> get oe94eic337slnjv1bvlreoa574 "username| S:7:\ "captain\"; "

The above is the whole content of this article, I hope that everyone's study has helped.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.