A brief analysis of how Redis implements Session in PHP distribution
- 2020-05-15 02:29:41
- OfStack
This article introduces the method of implementing Session by Redis in PHP distribution. Without further discussion, what are the two methods
Method 1:
Go to the configuration file php.ini, change it to the following, save and restart the service
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
Method 2:
Add the following content directly into the code:
ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://127.0.0.1:6379");
Note: if the connection password requirepass is set in the configuration file redis.conf, save_path needs to be written like this
tcp://127.0.0.1:6379?auth=authpwd
, otherwise an error will be reported when saving session.
Testing:
<?php
//ini_set("session.save_handler", "redis");
//ini_set("session.save_path", "tcp://127.0.0.1:6379");
session_start();
// deposit session
$_SESSION['class'] = array('name' => 'toefl', 'num' => 8);
// The connection redis
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
// check session_id
echo 'session_id:' . session_id() . '<br/>';
//redis The deposit session ( redis with session_id As a key, In order to string Form storage)
echo 'redis_session:' . $redis->get('PHPREDIS_SESSION:' . session_id()) . '<br/>';
//php To obtain session value
echo 'php_session:' . json_encode($_SESSION['class']);
conclusion
The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.