Skip to content

Instantly share code, notes, and snippets.

@arkenidar
Created May 9, 2024 08:57
Show Gist options
  • Save arkenidar/47ea13a97fb10f4e9e92313f585112f5 to your computer and use it in GitHub Desktop.
Save arkenidar/47ea13a97fb10f4e9e92313f585112f5 to your computer and use it in GitHub Desktop.
Using "Redis.io". Redis used as PHP session "save handler". Redis client from PHP to a Redis server.
<?php
// Redis client from PHP to a Redis server.
// https://marketplace.visualstudio.com/items?itemName=cweijan.vscode-database-client2
// Debian setup hints:
// sudo apt install redis
// sudo apt install pkg-php-tools # provides: pecl
// sudo apt install php-dev # provides: phpize
// sudo pecl install redis
$redis = new Redis();
$redis->connect('127.0.0.1');
// https://redis.io/docs/latest/develop/data-types/lists/
print($redis->rPush('chat_messages', 'a message')."\n");
print(json_encode($redis->lRange('chat_messages', 0, -1))."\n");
<?php
// Redis used as PHP session "save handler".
// simple counter to test sessions. should increment on each page reload.
// use from a client that stores session cookies such as a web-browser,
// or something like "curl" but configured so, or "postman" or alike.
// http://localhost:8080/redis-session.php
// php -S localhost:8080
// https://marketplace.visualstudio.com/items?itemName=cweijan.vscode-database-client2
// Debian setup hints:
// sudo apt install redis
// sudo apt install php-redis
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', 'tcp://127.0.0.1:6379');
// https://joshtronic.com/2013/06/20/redis-as-a-php-session-handler/
session_start(); // session cookie
$count = isset($_SESSION['count']) ? $_SESSION['count'] : 0;
$count += 1;
$_SESSION['count'] = $count;
print($count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment