dsf5989 2015-10-23 08:43
浏览 71

php node.js redis session share认证

i just use php and node.js and after search i found only way for Authentication with user

so first thing i change my php session save bath to redis

and this is my code for insert session

session_name("userID");

session_start();

$_SESSION["username"] = "hello world";

this is image for redis and its show the session

enter image description here and here is my my browser cookie

enter image description here

i just want to Authentication the session on node.js so if user login in php ... node.js send data to user else refuse user ... or send data from mysql by node.js

so here i build node.js server

enter image description here

every thing its work good i require socket on client and i send data to server ..

every time user send value by socket i want to Authentication the user if user login php (session found in redis) send data to user else ... send u must login first

my problem on how to check if the session in redis by node.js

  • 写回答

1条回答

  • dongqiao0953 2015-10-23 12:35
    关注

    You need to query your redis db for the session information, with something like given below

    //user.js
    

    Add user and its session in appsession hash map in redis

    exports.addUserSession = function (redisClient,userId,sessionId,callback){
    
          redisClient.hset('appsession',sessionId,userId,function(err,response){
               callback(err,response);
          });
    };
    
    
    exports.validateUserSession = function (redisClient,userId,sessionId,callback){
        redisClient.hget('appsession',sessionId,function(err,response){
                if(response==userId)
                {
                    callback(err,true);
                }
                else
                {
                    callback(err,false);
                }
        });
    };
    

    you can then use the same method in your code to check whether user session is available or not. E.g

    var session = require(./user.js );
    socket.on("selector", function( data) { 
            sessionID = data.handshake.session;
            USERNAME = socket.username;
            session.validateUserSession(redisClient,USERNAME,sessionID,function(err,response){
                // Do other processing if session is valiated
           }
    });
    

    Hope it helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题