2

I have an app developed entirely with PHP and now I want to integrate RTN (Real Time Notification System) in my app. Currently I have accomplished connecting to Node.js after page rendered by PHP with this code

SERVER:

var app = http.createServer(function(request,response)
{
   response.writeHead(200,{"Content-Type":"text/plain"});
   response.end();
}).listen(3000);

var io = require('socket.io').listen(3000);

CLIENT

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:3000');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

but now getting puzzled that how can I get the Request Header of client request? Actually I want to get Client Cookie using PHP for development and Node for RTN system. Now as I don't know how to get Request Header sot unable to get cookie as well :(

UPDATED I am using Memcache for storing Sessions and this is the main reason I want to get cookie so that I can get session information of PHP

2
  • How are you connecting to the node js server without declaring the port that the socket is bound too, 3000? Commented Mar 2, 2014 at 5:08
  • I have updated my code for better understanding Commented Mar 2, 2014 at 5:12

2 Answers 2

2

If you want to get PHP Session ID in Node.js then simply use this

io.sockets.on("connection",function(client)
{   
    console.log(client.handshake.headers.cookie);
});

it will return PHP session ID. This may work different if you are not using the way you mentioned above that is page generated by PHP and calling Node seperately. The output of above command would be something like this

PHPSESSID=aj233jkjdfbk13adlljkwlkjjkj002

This is because PHP has set its cookie and you are getting the headers.cookie which returns ID and that's PHP's Session ID.

Sign up to request clarification or add additional context in comments.

Comments

0

Server generated session cookies have "HTTPOnly" flag and you cannot read it on client-side or on any other server, you can only read it on original server. Client browsers send session cookie only to original server,

So if have mydomain.com on PHP and your Nodejs is on mynode.com, mynode.com will not receive session cookie generate by mydomain.com

You need to share session between php and nodejs take a look at this: http://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/

1 Comment

I am sharing session with Memcached and this is the reason I want to get cookie so that I get session information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.