问题
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