For node (and Express 4.x):
Start with the example from express-session, but use connect-redis as your session store instead.
Example code:
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:
Use a redis session handler like redis-session-php.
Example code:
<?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);
?>
Note: Make sure you use the same prefix
(connect-redis)/REDIS_SESSION_PREFIX
(redis-session-php) (connect-redis uses 'sess:' and redis-session-php uses 'session:php:' by default) and ttl
(connect-redis)/session.gc_maxlifetime
(PHP) (and same database if you are using a redis database other than the default) for both redis-session-php and connect-redis.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…