For node (og Express 4.x):
Start med eksemplet fra express-session, men brug connect-redis som din sessionsbutik i stedet.
Eksempelkode:
var express = require('express'),
app = express(),
cookieParser = require('cookie-parser'),
session = require('express-session'),
RedisStore = require('connect-redis')(session);
app.use(express.static(__dirname + '/public'));
app.use(function(req, res, next) {
if (req.url.indexOf('favicon') > -1)
return res.send(404);
next();
});
app.use(cookieParser());
app.use(session({
store: new RedisStore({
// this is the default prefix used by redis-session-php
prefix: 'session:php:'
}),
// use the default PHP session cookie name
name: 'PHPSESSID',
secret: 'node.js rules'
}));
app.use(function(req, res, next) {
req.session.nodejs = 'Hello from node.js!';
res.send(JSON.stringify(req.session, null, ' '));
});
app.listen(8080);
For PHP:
Brug en redis session-handler som redis-session-php.
Eksempelkode:
<?php
// from https://github.com/TheDeveloper/redis-session-php
require('redis-session-php/redis-session.php');
RedisSession::start();
$_SESSION["php"] = "Hello from PHP";
// `cookie` is needed by express-session to store information
// about the session cookie
if (!isset($_SESSION["cookie"]))
$_SESSION["cookie"] = array();
var_dump($_SESSION);
?>
Bemærk:Sørg for at bruge det samme prefix
(connect-redis)/REDIS_SESSION_PREFIX
(redis-session-php) (connect-redis bruger 'sess:' og redis-session-php bruger 'session:php:' som standard) og ttl
(connect-redis)/session.gc_maxlifetime
(PHP) (og samme database, hvis du bruger en anden redis-database end standarden) for både redis-session-php og connect-redis.