Redis Alternative File Store session statement in PHP

Source: Internet
Author: User
Tags session id redis server

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 function of PHP session_set_save_handler 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


Parameters
Describe
Open
This function is called when the session is opened. Receive two parameters, the first parameter is to maintain the path of the session, the second parameter is the name of the session
Close
This function is called when the session operation is complete. No parameters are received.
Read
The session ID is used as the parameter. The data is obtained from the data store by the session ID, and this data is returned. If the data is empty, you can return an empty string. This function is triggered before calling Session_Start
Write
Called when the data is stored. There are two parameters, one is the session ID, the other is the session data
Destroy
The Destroy function is triggered when the Session_destroy function is called. 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:

Redis = new Redis ();//Create Phpredis instance
$this->redis->connect (' 127.0.0.1 ', 6379);//Connect Redis
$this->redis->auth ("107lab");//Authorization
$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) {
$value = $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)) {//With session ID key, store $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;
}
Public function Destroy ($id) {
if ($this->redis->delete ($id)) {//delete the specified record in Redis return true;
}
return false;
}
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

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

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 *
1) "oe94eic337slnjv1bvlreoa574"
127.0.0.1:6379> Get oe94eic337slnjv1bvlreoa574
"Username|s:7:\" captain\ ";"

Redis Alternative File Store session statement in PHP

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.