Configure PHP session storage to Redis for multiple servers sharing session

Source: Internet
Author: User
Tags fpm php session

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

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.