This article introduces the session operation of php-redis, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
First configure the PHP parameters, the following 2 methods
Modify the php.ini file directly
session.save_handler = redis session.save_path = "tcp://127.0.0.1:6379"
Add the following content to the code page header:
ini_set("session.save_handler", "redis"); ini_set("session.save_path", "tcp://127.0.0.1:6379");
Note: If the 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 the error cannot be reported and an error will be reported
<?php //ini_set("session.save_handler", "redis"); //ini_set("session.save_path", "tcp://127.0.0.1:6379"); session_start(); //存入 session$_SESSION['class'] = array('name' => 'Alicelock', 'num' => 21); //连接 redis$redis = new redis(); $redis->connect('127.0.0.1', 6379); //检查session_idecho 'session_id:' . session_id() . '<br/>'; //redis存入的session(redis用session_id作为key,以string的形式存储)echo 'redis_session:' . $redis->get('PHPREDIS_SESSION:' . session_id()) . '<br/>'; //php获取session值echo 'php_session:' . json_encode($_SESSION['class']);
Related recommendations:
Php-Redis Installation test notes
The above is the detailed content of Session operation of php-redis. For more information, please follow other related articles on the PHP Chinese website!