Session cross-Domain shared solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.