Alternate method of Session storage Redis

Source: Internet
Author: User
Tags session id php class redis redis server

PHP defaults to using file storage session, if the concurrent volume is large, the efficiency is very low. and Redis support for high concurrency is excellent, so you can use Redis instead of file storage session.
Here, introduce the function of PHP Session_set_save_handler function and use method. This function defines a user-level session save function (such as open, close, write, and so on). The prototype is as follows:

BOOL Session_set_save_hanler (callback Open,callback close,callback read,callback write,callback destory,callback GC)

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

The following is an instance of using Redis to store the session.

Write a session management class SessionManager with the following code:

<?php class SessionManager {private $reids;
    Private $sessionSavePath;
    Private $sessionName;

    Private $sessionExpireTime = 300;
        Public Function __construct () {$this->reids=new redis ();

        $this->reids->connect (' 127.0.0.1 ', 6379); $retval =session_set_save_handler (Array ($this, "open"), Array ($this, "close"), Array ($th 
IS, "read"), Array ($this, "write"), Array ($this, "destroy"), Array ($this, "GC"));

Public function open ($path, $name) {return true;}

Public function Close () {return true;}
    The public function read ($id) {$vale = $this->reids->get ($id);
    if ($vale) {return $vale;
    }else {return "; The Public function write ($id, $data) {if ($this->reids->set ($id, $data)) {$this->reids->expire ($i
        D, $this->sessionexpiretime);
    return true;
return false; } Public Function Destroy ($id) {if ($this->reids->delete ($id)) {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 the Redis server, set the session callback function using the Session_set_save_handler function, and invoke the Session_Start function to open the session function. Because the open, close, and GC callback functions in this example are not very large, return true directly.
In the write callback function, with the session ID as the key, the session's data is stored as value to the Redis server, and the session expiration is set to 300 seconds. In the read callback function, read the data from the Redis server with the session ID as key and return this data. When the destroy callback function is heavy, the corresponding session data is deleted from the Redis server with the session ID as key.

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

<?php
    include ("sessionmanager.php");
    New SessionManager (); Open Session Management
    $_session[' username ']= ' hezikuang ';//create Session variable
?>

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

<?php
    include ("sessionmanager.php");
    New SessionManager (); Open Session Management
    echo $_session[' username '];
? >

You can visit session_get.php to see if you are successful.

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.