Detailed explanation of using redis to store session instances in php-PHP source code

Source: Internet
Author: User
Session is a global variable that can be used to run across pages and only on the server. It is used for data security verification, the following is an example of using redis to store session instances in php. If you are interested, please refer to it. Session is a global variable that can be used to run across pages and only on the server. It is used for data security verification, the following is an example of using redis to store session instances in php. If you are interested, please refer to it.

Script ec (2); script

Phpinfo, you can see the session storage, you can use files user memcache redis, the advantage of using the database to store sessions is that compared with the file storage, the speed is faster and the performance is better in a large number of users, in addition, for a distributed system, you must use a database to store sessions. Here we will summarize the two methods for storing session in redis.
We should have used mysql to store sessions. The principle of the session lifecycle is actually determined by the two parameters session. gcprobability and session. gcpisor and the maximum life time. Each php request has a certain probability of triggering the session detection mechanism. Session_setsavehandler can be used to redefine session behavior. There are two ways to implement redis storage session

Code Example 1:

The Code is as follows:

Ini_set ('session. gc_maxlifetime', 3600 );
Ini_set ("session. save_handler", "redis ");
Ini_set ("session. save_path", "tcp: // FIG: 6379? Auth = authpwd ");
Session_start ();
// $ _ SESSION ['session'] = 'this is session content! ';
Echo $ _ SESSION ['session '];
Echo session_id ().'
';

$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
// Redis uses session_id as the key and stores it as a string
Echo $ redis-> get ('phpredis _ SESSION: '. session_id ());
?>

Code Example 2:

The Code is as follows:

Class RedisSessionHandler {
Public $ ttl = 1800; // 30 minutes default
Protected $ db;
Protected $ prefix;
Public function _ construct ($ db, $ prefix = 'phpsessid1: ', $ time = 1800 ){
$ This-> db = $ db;
$ This-> prefix = $ prefix;
$ This-> ttl = $ time;
}
Function _ open ()
{
//
}

Function _ close ()
{
$ This-> db = null;
Unset ($ this-> db );
}

Function _ read ($ id)
{
$ Id = $ this-> prefix. $ id;
$ SessData = $ this-> db-> get ($ id );
$ This-> db-> expire ($ id, $ this-> ttl );
Return $ sessData;
}

Function _ write ($ id, $ data)
{
$ Id = $ this-> prefix. $ id;
$ This-> db-> set ($ id, $ data );
$ This-> db-> expire ($ id, $ this-> ttl );
}

Function _ destroy ($ id)
{
$ This-> db-> del ($ this-> prefix. $ id );
}

Function _ clean ($ max)
{
//
}
}
$ Redis = new Redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> select (1 );
$ SESSION_ID_PREFIX = 'rsid :';
$ SESSION_MAX_TIME = 1440;
Ini_set ('session. gc_maxlifetime ', $ SESSION_MAX_TIME );
$ SessHandler = new RedisSessionHandler ($ redis, $ SESSION_ID_PREFIX, $ SESSION_MAX_TIME );
Session_set_save_handler (
Array ($ sessHandler, '_ open '),
Array ($ sessHandler, '_ close '),
Array ($ sessHandler, '_ read '),
Array ($ sessHandler, '_ write '),
Array ($ sessHandler, '_ destroy '),
Array ($ sessHandler, '_ clean ')
);

Session_start ();
Echo session_id ();
Echo $ _ SESSION ['name'] = 'hangzhou ';

> The first method is relatively simple, but the database number cannot be selected, and the prefix information of session_id is difficult to control. However, this method can also directly modify the php configuration file so that sessions of all programs use redis.
> The second method is relatively troublesome, but it is easy to use.
> When we talk about concurrency consistency on the Internet, all hash algorithms will certainly have this problem, but it can be solved to some extent through the ip prefix hash combination.

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.