php Session method of storing to Redis

  • 2020-11-03 22:02:41
  • OfStack

Of course, to install the php extension, see this article: Redis and PHP extension installation
Modify the Settings of php.ini


session.save_handler = redis
session.save_path =  " tcp://127.0.0.1:6379 " 
 Restart after modification php-fpm or nginx . phpinfo()

session redis
This can be done if you do not want to modify php. ini


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

If the configuration file /etc/ redis.conf contains the connection password requirepass, it will report an error when saving session. save_path writes tcp://127.0.0.1:6379? auth = authpwd can.
Some netizens mentioned that the value of session, redis or memcache storage session has the concurrency 1 problem, which has not been tested in detail.

<?php
// If not modified php.ini The following two lines are commented 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 = new redis();
$redis->connect('127.0.0.1', 6379);
//redis with session_id As a key And based on string Formal storage of 
echo $redis->get('PHPREDIS_SESSION:' . session_id());
 ?>
 


Related articles: