Home > Article > Backend Development > php Redis storage session
Redis is a high-performance key-value database. The emergence of redis can play a very good supplementary role to relational databases in some situations. On the other hand, many people choose redis for session storage. It compensates for the speed and efficiency of file storage to achieve a higher availability.
Modify the settings of php.ini
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
Restart php-fpm after modification, phpinfo() can view the session Stored in redis.
If you don’t want to modify php.ini, you can add in the code:
ini_set("session.save_handler","redis");
ini_set("session.save_path","tcp://127.0.0.1:6379 ");
If redis.conf sets a connection password (requirepass), the save_path of the session needs to be modified to the value of: tcp://127.0.0.1:6379?auth=requirepass.
If you choose redis database, session.save_path = "tcp://xx.xx.xx.xx:6789?database=11", etc.
View the value of redis storage session:
session_start();
$_SESSION['sessionid']='www.webyang.net';
$redis =new redis();
$redis->connect('127.0.0.1',6379);
//redis uses PHPREDIS_SESSION: add session_id as key, and store it in the form of string
echo $redis->get('PHPREDIS_SESSION:' .session_id());//Output www.webyang.net
?>
I saw on the Internet that some people questioned the problem of concurrency consistency when redis stores session values (file storage sessions are handled by file locks), this is There is no research.