Cross-domain name cookie implementation - simple alternative implementation

zhaozj2021-02-17  46

Cross-domain name cookie implementation - simple alternative implementation

Preamble:

Before: Recently, I saw that many colleagues posted inquiry on the forum how to implement cookies across domain names.

An old tutorial problem, a reasonable requirement and technical restrictions, I think of a lot of people, so I have a simple alternative implementation that I have done with you.

text:

First talk about the process

A user first visits A website, the SETCOOKIE program of the A website sets several cookie variables at the client.

The user accesses the B website, directly accesses the getCookie program of the B website, this time the getCookie program does not receive the cookie variable of the client, at which time the B website gets to get to the GetCookie of the A website to read the same name of the client Cookie variables (of course, these variables are originally set in the first step.), The A website reads the data to the B website via the address.

The following uses the code use PHP as a program language.

First we write a simple setCookie program,

Cookiecrosssite_setValue.php

/ **

* Program name: cookiecrossSite_setValue.php

* Author: Eric Li (CN-SZ ISD)

* Date CREATED:

* DESCRIPTION:

* Revision History (Verdion, ModiFid by, Date of change, Comments of change)

*

** /

SetCookie ("Cookie_user_ID", "800800800", TIME () 10800);

SetCookie ("Cookie_user_name", "Ericli", Time () 10800);

SetCookie ("Cookie_user_type", "VIP", TIME () 10800);

?>

There is nothing to say above this setCookie program, and we will look at the following file.

CookiecrossSite_GetValue.php

/ **

* Program name: cookiecrossSite_getValue.php

* Author: Eric Li (CN-SZ ISD)

* Date CREATED:

* DESCRIPTION:

* Revision History (Verdion, ModiFid by, Date of change, Comments of change)

*

** /

$ URL = "";

$ domain1 = "

Www.domain1.com "; // assigns the value according to your debug environment.

$ domain2 = "

Www.domain2.com ";

IF ($ comfrom == $ domain1) {

// display the variable.

Echo "cookie_user_id =". $ cookie_user_id. "
";

Echo "cookie_user_name =". $ cookie_user_name. "
";

Echo "cookie_user_type =". $ cookie_user_type. "
";

} else {

IF (count ($ http_cookie_vars) == 0) {

Header ("Location: http: //". $ domain1. "/cookiecrosssite_getvalue.php?comefrom=". $ domain2);} else {

While (List ($ Name, $ Value) = Each ($ http_cookie_vars) {

IF ($ VALUE == "") {

} else {

IF ($ comFrom! = "$ domain2") {

// Display All Cookie Variables.

Echo "Name =". $ name. "| value =". $ value. "
";

}

// generate a url string.

// This can be appropriately added to the URLENCode.

$ URL. = $ Name. "=". $ value. "&";

}

}

}

IF ($ comfrom == $ domain2) {

Header ("Location: http: //". $ domain2. "/cookiecrosssSite_getvalue.php?comeFrom=". $ domain1. "&". $ URL);

}

}

?>

The above code has been tested.

Test premise:

Put two files in the root directory of a domain

Cookiecrosssite_setValue.php

CookiecrossSite_GetValue.php

Release a file in the B domain root directory

CookiecrossSite_GetValue.php

Test steps:

First access the a domain cookiecrossSite_SetValue.php

Then access cookiecrossSitE_GetValue.php of B domain name

supplement:

Of course, the code above is just a different way of implementation. In order to understand the process more clearly, the data does not encode the data when the parameter is passed on the URL.

If you want to implement a cookie between multiple domain names, then it is best,

Use one of the domain names, when setcookie, the programs of several other websites jump to the transit server to do setcookies, then all getCookies, just need to go to the program under this domain name. Yes.

Liyujie2000@sohu.com

转载请注明原文地址:https://www.9cbs.com/read-28573.html

New Post(0)