php Session可以保存到文本或者内存、还有数据库,今天讲的是存到Redis的方法。
当然要写先安装php的扩展,,可参考这篇文章:Redis及PHP扩展安装
修改php.ini的设置
复制代码 代码如下:
session.save_handler = redis
session.save_path = “tcp://127.0.0.1:6379″
修改后重启php-fpm或nginx,phpinfo()
session redis
如果不想修改php.ini可这样
复制代码 代码如下:
ini_set(“session.save_handler”,”redis”);
ini_set(“session.save_path”,”tcp://127.0.0.1:6379″);
如果配置文件/etc/redis.conf里设置了连接密码requirepass保存session的时候会报错,save_path这样写 tcp://127.0.0.1:6379?auth=authpwd 即可。
有网友提到相对于文件存储的session,redis或者memcache存储session值会有并发一致性的问题,这块并没有细测过。
复制代码 代码如下:
//如果未修改php.ini下面两行注释去掉
//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用session_id作为key并且是以string的形式存储
echo $redis->get('PHPREDIS_SESSION:' . session_id());
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.