PHP implements cross-domain Cookie

Source: Internet
Author: User
Cookie is a huge invention. it promises web developers to keep the login status of their users. However, when your site or network has more than one domain name, the title will be displayed. In terms of Cookie specification, a cookie can only be used for one domain name Cookie is a huge invention, and it promises web developers to keep the login status of their users. However, when your site or network
When there is more than one domain name, the title will be displayed.


In terms of Cookie specification, a cookie can only be used for one domain name and cannot be sent to other domain names. Therefore, in the browser
A cookie is set for a domain name, which is invalid for other domain names. If you want your users
You can log on to another domain name, which is really hard.


My solution will apply the following general framework:

A preset script will be used to receive the sessionid passed through the GET or COOKIE method. It will give priority to GET than COOKIE
Variable. Therefore, whenever cross-domain names need to be referenced, sessionid is sent as a URL parameter.
Modify Apache configuration to rewrite all cross-domain cookies. The reason for doing so will be clear soon.
Apply the variable when a cross domain name is referenced at any time.
Step 1: Create a preset script
Add the following code to the preset script (or present it in the functions before all scripts ).


/* Support cross-domain cookie ...*/

// If The GET variable has been set and is different from the cookie variable
// Apply the get variable (update cookie)
Global $ HTTP_COOKIE_VARS, $ HTTP_GET_VARS;
If (isset ($ sessionid) & isset ($ HTTP_GET_VARS [\ 'sessionid \ ']) & ($ HTTP_COOKIE_VARS [\ 'sessionid \']! = $ HTTP_GET_VARS [\ 'sessionid \ ']) {
SetCookie (\ 'sessionid \ ', $ HTTP_GET_VARS [\ 'sessionid \'], 0 ,\'/\',\'\');
$ HTTP_COOKIE_VARS [\ 'sessionid \ '] = $ HTTP_GET_VARS [\ 'sessionid \'];
$ Sessionid = $ HTTP_GET_VARS [\ 'sessionid \ '];
}

?>

Once the code is run, a global \ 'sessionid \ 'variable can be used in the script. It will keep
Sessionid value, or the sessionid value sent through GET request.


Step 2: Reference application variables for all cross domain names
Create a global configuration file to store the basic reference situation of the domain name that can be switched. For example, if we have
Domain1.com and domain2.com are set as follows:


$ Domains [\ 'domain1 \ '] = 'http: // www.domain1.com/-sharesessionid -';
$ Domains [\ 'domain2 \ '] = 'http: // www.domain2.com/-sharesessionid -';

?>

Now, let's do the following in the code:


Echo 'click here to contact us .';

?>
You will generate the following output:

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.