Session operation of php-redis

不言
Release: 2023-03-24 12:20:01
Original
1800 people have browsed it

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

Configuring PHP


First configure the PHP parameters, the following 2 methods

Method 1:

Modify the php.ini file directly

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
Copy after login

Method 2:

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");
Copy after login

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

Page test

<?php
//ini_set("session.save_handler", "redis");
//ini_set("session.save_path", "tcp://127.0.0.1:6379");
session_start();
//存入
session$_SESSION[&#39;class&#39;] = array(&#39;name&#39; => &#39;Alicelock&#39;, &#39;num&#39; => 21);
//连接
redis$redis = new redis();
$redis->connect(&#39;127.0.0.1&#39;, 6379);
//检查session_idecho &#39;session_id:&#39; . session_id() . &#39;<br/>&#39;;
//redis存入的session(redis用session_id作为key,以string的形式存储)echo &#39;redis_session:&#39; . $redis->get(&#39;PHPREDIS_SESSION:&#39; . session_id()) . &#39;<br/>&#39;;
//php获取session值echo &#39;php_session:&#39; . json_encode($_SESSION[&#39;class&#39;]);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!