Home >Backend Development >PHP Tutorial >How to store php Session in Redis_PHP Tutorial

How to store php Session in Redis_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:34946browse

Of course, you need to install the php extension first. You can refer to this article: Redis and PHP extension installation
Modify the settings of php.ini

Copy the code The code is as follows:

session.save_handler = redis
session.save_path = “tcp://127.0.0.1:6379″
After modification, restart php-fpm or nginx, phpinfo ()

session redis
If you don’t want to modify php.ini, you can do this

Copy the code The code is as follows:

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

If the configuration file /etc/ If the connection password requirepass is set in redis.conf, an error will be reported when saving the session. In save_path, write tcp://127.0.0.1:6379?auth=authpwd.
Some netizens mentioned that compared to file storage sessions, redis or memcache will have concurrency consistency issues when storing session values. This has not been tested in detail.
Copy code The code is as follows:

//If the following two lines of php.ini are not modified Remove the comments
//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 '
';

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
//redis uses session_id as the key and is stored in the form of string
echo $redis->get('PHPREDIS_SESSION:' . session_id());
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825000.htmlTechArticleOf course, you need to install the php extension first. You can refer to this article: Redis and PHP extension installation and modification php.ini The setting copy code is as follows: session.save_handler = redis session.save_path...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn