How to configure PHP session is stored in redis

When site users increased amount of time, the normal session there will be a bit slow access problems, if we raise the speed yet.

We can use redis to save the session information session.

PHP sessions default is in the form of a file can be configured to NoSQL, that improves access speed, and can achieve a good session sharing. 

 

Configuration is as follows:

 Method a: modified php.ini setting

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379?auth=youpwd"

After modifying, restart the php-fpm.

 

Second way: by the ini_set () function sets

ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://127.0.0.1:6379?auth=youpwd");

 

If the profile set when in /etc/redis.conf connection password requirepass, save the session will complain, save_path write tcp: //127.0.0.1:? 6379 auth = authpwd can.

 

Test code:

? < PHP
 // if the following two lines of unmodified php.ini Notes removed 
the ini_set ( 'session.save_handler', 'Redis' );
 the ini_set ( 'the session.save_path', 'TCP: //127.0.0.1: 6379' ); 
 
session_start ();
 $ _SESSION [ 'SessionID'] = '! the this IS the session Content' ;
 echo  $ _SESSION [ 'SessionID' ];
 echo 'a' ; 
 
$ Redis = new new Redis ();
 $ Redis -> Connect ( '127.0.0.1', 6379 ); 
 
// Redis session_id used as the key string is stored in the form and 
echo  $ Redis -> GET ( 'PHPREDIS_SESSION:' . session_id());

 

Guess you like

Origin www.cnblogs.com/yipianchuyun/p/11795585.html