PHP sessions are in the form of files by default and can be configured into NoSQL for session sharing, such as sharing sessions in a server cluster
The configuration is as follows:
Method One: Modify the settings of the php.ini
Session.save_handler ="tcp://127.0.0.1:6379"
After modifying, restart the PHP-FPM.
Pkill php-fpmphp-fpm
Mode two: set by Ini_set () function
Ini_set ("Session.save_handler", "Redis"); Ini_set ("Session.save_path", "tcp://127.0.0.1:6379");
If the configuration file/etc/redis.conf set the connection password Requirepass, save the session will be error, Save_path so write tcp://127.0.0.1:6379?auth=authpwd can.
Test code:
<?PHP//if PHP.ini is configured, the following two lines comment out Ini_set (' Session.save_handler ', ' Redis '); Ini_set (' Session.save_path ', ' tcp:// 127.0.0.1:6379 '); Session_Start();$_session[' sessionid '] = ' This is session content! ';Echo $_session[' SessionID '];Echo' <br/> '; $redis=NewRedis ();$redis->connect (' 127.0.0.1 ', 6379); //Redis uses session_id as the key and is stored as a stringEcho $redis->get (' phpredis_session: '.session_id());
Configure PHP session storage to Redis for multiple servers sharing session