use same php session on another website on link click

我的梦境 提交于 2020-01-04 02:45:14

问题


I have a question. Let's say I am on website domain1.com and I click on a link on domain1.com that brings me to domain2.com. But I want to use the same session_id() that was set with session_start() in domain1.com on domain2.com. How do I accomplish this in PHP?

Thank you.


回答1:


As the session_id() itself doen't contain any data, there are a couple of things to consider:

  • If the session is set in a cookie, you can't transfer it (unless to a subdomain), make sure you transfer it via GET
  • In order to have the data associated with the SESSION sync accross the servers, you have to share the file system (e.g. via GlusterFS), at least for the path the SESSIONS are saved (e.g. /tmp/php)
  • if you save objects in the SESSION make sure they are available on both servers

If both domains are pointing to the same document root on the same server you only need to take care about the first point

If you only want to use the session_id() and not the associated data, you can just set the id: session_id('yourSessionIdFromDomain1')




回答2:


Maybe you can use a MySql Database which you can access from both Sites...

Like:

Table _sessions -> Fields('access_token','session_vars')

in session_vars you can save $_SESSION each time a site loads. access_token should be a unique string who is generated when the user comes to the Website first.

This is very dangerous!! 11 :-)



来源:https://stackoverflow.com/questions/8540725/use-same-php-session-on-another-website-on-link-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!