HiDd3N
HiDd3N

Reputation: 504

nodejs cant read phpsessid and return error undefined

i just did like this article to read session in nodejs and read sessions data from memcached.but i had some problem to read data back from phpsessid.when i node this index.js script

var app = require("http").createServer(handler),
    fs = require("fs"),
    memcache = require("memcache"),
    co = require("./cookie.js");

app.listen(7070);

//On client incomming, we send back index.html
function handler(req, res){
    fs.readFile(__dirname + "/index.html", function(err, data){
        if(err){
            res.writeHead(500);
            return res.end("Error loading index.html");
        }else{
            res.writeHead(200);
            res.end(data);
        }
    });


    //Using php session to retrieve important data from user
    var cookieManager = new co.cookie(req.headers.cookie);

    var client = new memcache.Client(11211, "localhost");
    client.connect();

    client.get("sessions/"+cookieManager.get("PHPSESSID"), function(error, result){
        console.log("error : "+error);
        if(typeof(error)==="undefined"){
            var session = JSON.parse(result);
        }
    });
}

and access this script console log show this error : undefined message.i want to access session data and need help in this.

thanks in advance

Upvotes: 2

Views: 2160

Answers (3)

Deisss
Deisss

Reputation: 189

No need to put connect for such a easy system. And btw this cookie manager is in production at many companies without any bug reported. So the problem does not come from that part.

Try to check thoose :

1) Check the php.ini file to find what is the session's cookie name (by default it is PHPSESSID, but it can be changed)

2) Check memcache is running, got everything fine (not locked by firewall or something). For that, on php or node.js side (choose one) : put a var into memcache then retrieve it and check the var content is the good one...

3) Check memcacheSessionHandler on PHP Side is correctly used (before everything you need to have it running)

3) If you still have problem, on cookie manager you will find a list function : print the content of it to check which cookie Node.JS Recieve.

Upvotes: 1

Chris
Chris

Reputation: 4225

This is basically the same question you asked before, try this:

client.get("sessions/"+cookieManager.get("PHPSESSID"), function(error, result) {
  if (error) {
    console.log(error);
  } else {
    session_management(JSON.parse(result));
  }
});

function session_management(session) {
  console.log(session);
}

Upvotes: 1

randunel
randunel

Reputation: 8945

The easiest way is to use the connect module with its cookie parser.

Upvotes: 0

Related Questions