To allow the session to be shared across domains, there are three issues that need to be resolved:
1, through what method to pass session_id? 2. What is the method to save session information? 3. What is the method for cross-domain
First, there are 4 ways to pass session_id
1. Through cookies
2, set the php.ini in the session.use_trans_sid=1, let PHP automatically cross-page pass session ID
3. Manually only son values by URL or hidden table
4, by file or database mode, in the other key corresponding to the value
Ii. 3 Ways to save session information
1. Database
2, Memcache
3. Sharing files
Third, the method of cross-domain access
1. Via Server (PHP script)
2. Through JavaScript
Due to various reasons. The scenario I chose is:
1. Cookie Transfer session_id
2, file preservation Sesson Information
3. Cross-domain through server scripting
Don't say anything, on the code. These two pieces of code are set in PHP script
To set domain names that allow cross-domain:
$origin=isset($_server[' Http_origin '])?$_server[' Http_origin ']: '; $allow _origin=Array( ' http://two.google.com ', ' http://three.google.com ');Header("Access-control-allow-credentials:true");if(In_array($origin,$allow _origin)){ Header(' Access-control-allow-origin: '.$origin); }
Set up a cross-domain cookie
if(!isset($_cookie[' Session_idx '])){ $session _id=session_id(); Setcookie("Session_idx",$session _id, Time() +3600*24*365*10, "/", ". Google.com");//here. The role of Google.com is to let the www.google.com, two.google.com and other two-level domain names can be shared this cookie $_session[' userName '] = ' OK ';}Else{ session_id($_cookie[' Session_idx ']);}Echo $_session[' UserName '];
This scenario, there is no cross-server flaw. There are still some pits in the cross-domain that need to be explored.
Reference URL:
http://blog.163.com/lgh_2002/blog/static/4401752620105810501715/
http://tw5566.iteye.com/blog/1945825
Session cross-Domain shared solution