Transferring a session through a server in PHP

I need to transfer user session over servers. i.e. If the user is logged on to server1, and if the user exists on server2, then I need to transfer the user session data to server2. For this, I used the following technique

From server1, redirect the user to http: //server2/auth_from_server1.php? Sessionid = 12345 On server2 (internally, in the auth_from_server1.php PHP code), execute the request http: //server1/secret/check_session_id.php with sessionid, 12345. On server1, in the implementation of check_session_id.php, check the identifier and return OK, FAILURE and the session-related data that you want to transfer, for example, username, ... On server2, when the call returns with OK, save the transferred session data and give the user a cookie and a session for this server.

But when the callback function calls auth_from_server1.php, the value in the session identifier is null. I tried to check sessionid as

if(isset($_SESSION['sessionId'])) echo 'true'; else echo 'false'; 

But $ _SESSION ['sessionId'] is null. On the login page, I set the value for the session id as

 $_SESSION['sessionId'] = session_id(); 

Thanks in advance....

+8
php session sessionid
source share
6 answers

Wouldn't it be easier to just store session data in a shared directory?

Alternatively, you can save it to a database.

+16
source share

I would suggest writing your own PHP session handler or using a pre-processed session handler like ShareDance . You can store session information in a MySQL database shared between two machines, SQLite file, etc.

There are many advantages to using PHP, which is built into the ability to get / set session data using session_set_save_handler (), namely that all calls to get or set from $ _SESSION will work in your application code without any additional changes.

More information can be found here: http://php.net/manual/en/function.session-set-save-handler.php

A tutorial on writing custom session handlers (old but still relevant) is here: http://devzone.zend.com/article/141

+7
source share

When server 2 calls server1 / secret / check_session_id.php, should it pass in a session identifier such as server1 / secret / check_session_id.php? sessionid = 12345, and in check_session_id.php you need to call session_id($_GET['sessionid']) before session_start() .

+3
source share

Another possibility is file system exchange. Since PHP places the session on the file system, you can share the file system (e.g. sshfs) on both servers.

the parameter for changing the destination directory in php.ini is

 session.save_path 
+3
source share

I think storing the user ID in the database is the most appropriate way to do this. This is a way to check for errors.

Greetings

+2
source share

This problem can quickly get out of hand when you start adding more and more problems to the problem. One of the best solutions I have found is to save the session to a database and share this database with the servers. Redis is usually great for this. Here is a great guide to get started with redis

+2
source share

Source: https://habr.com/ru/post/650083/


All Articles