对于节点(和Express 4.x):
从express-session的示例开始,但改用connect-
redis作为会话存储。
示例代码:
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);
对于PHP:
使用redis会话处理程序,例如redis-session-php。
示例代码:
<?php// from https://github.com/TheDeveloper/redis-session-phprequire('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 cookieif (!isset($_SESSION["cookie"])) $_SESSION["cookie"] = array();var_dump($_SESSION);?>
注意:请确保使用相同的
prefix(connect-redis)/
REDIS_SESSION_PREFIX(redis-session-
php)(默认情况下,connect-redis使用’sess:’,redis-session-
php使用’session:php:’)和
ttl(connect- redis)/
session.gc_maxlifetime(PHP)(如果使用redis数据库而不是默认数据库,则为同一数据库),用于redis-session-php和connect-redis。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)