Zenth
Zenth

Reputation: 811

Comunicate NodeJs with PHP and viceversa

I put in situation:

I have a website entire make in PHP 5.3 and MYSQL, the site need to user to login for get access, the login "simply" check user/password and create a $_SESSION in the domain with the user ID and other user non-personal data.

In PHP i need to read this $_SESSION to detect if user is logued.

Now, i think in create a NodeJS real-time chat with websockets (only work in last browsers obiously, but i looking for pure HTML5 site, not external client-js like socketio.js), but here is my problems:

First problem I need to get the $_SESSION['user'] in the NodeJS, for make this i need to "pull" from PHP TO NodeJS, send a message like "update-this-user-auth" with the $_SESSION['user'] data, but the problem is, first HOW is the best way to pull from PHP Server to NodeJS Server runing in the same (or not..) machine.

And second problem HOW identify the user in NodeJS, because the user have $_SESSSIOn in PHP but i dont know if the request is from user nº1, nº32 or nº 999999.

For the problem of the comunicate from PHP to NodeJS I read some posts, and get 2 ways:

CURL User, usin PHP Curl to "call" a NodeJS service, and send-read data from PHP to NodeJS Sending messages from PHP to Node.js

DNODE, i found this googling, have good look, but require some extra librarys, and i like to make the code clear and preferably simple. http://bergie.iki.fi/blog/dnode-make_php_and_node-js_talk_to_each_other/

I thanks to all ideas and comments for the best solution to this two problem.

Upvotes: 1

Views: 272

Answers (1)

Salvatorelab
Salvatorelab

Reputation: 11873

With PHP:
You can save a random generated key in the database, associated with user's ip, user id and any other session information (time created, last active, expiration date...). Also, save that key in a cookie too.

With Node.js:
Read the cookie, find the key in the DB and check if the session is valid or not.

So, basically, instead of storing all info in PHP, use a shared storage like for example a DB.

Upvotes: 2

Related Questions