Transfer session across server in PHP

后端 未结 6 1013
陌清茗
陌清茗 2021-01-12 03:38

I need to transfer the user session across servers. ie. If user logged in server1 and if the user exists in server2 , then I have to transfer the user session details to ser

相关标签:
6条回答
  • 2021-01-12 03:59

    I would suggest writing a custom PHP session handler or using a prebuilt session handler like ShareDance. You can store session information in a MySQL database shared amongst the two machines, a SQLite file, etc.

    There are many benefits to using PHP's built in ability to get/set session data by using session_set_save_handler() namely, that all calls to get or set from $_SESSION will work in your application code without any additional modification.

    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

    0 讨论(0)
  • 2021-01-12 03:59

    Another opportunity could be sharing the filesystem. Since PHP puts the session in the filesystem, you could share the filesystem (sshfs for example) on both servers.

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

    session.save_path
    
    0 讨论(0)
  • 2021-01-12 04:02

    When server 2 calls server1/secret/check_session_id.php it has to submit the session ID like server1/secret/check_session_id.php?sessionid=12345 and in check_session_id.php you have to call session_id($_GET['sessionid']) before session_start().

    0 讨论(0)
  • 2021-01-12 04:04

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

    0 讨论(0)
  • 2021-01-12 04:04

    I think to store an id of a user in DB is the most appropriate way to do this.It is an error proof way.

    Cheers

    0 讨论(0)
  • 2021-01-12 04:16

    Wouldnt it be easier to just store session data in a shared directory?

    Alternatively you can store it in a database.

    0 讨论(0)
提交回复
热议问题