PHP application session performs synchronous login on two different server websites

Source: Internet
Author: User
Tags php session
PHP uses session to synchronously log on to two different server websites. now there are two servers a: www. test1.comb: www.test2.com I want to log on to test1, and then log on to test2 as well. how can I write this? I have partially tested PHP using session to perform synchronous login on two different server websites.
There are now two servers
A: www.test1.com
B: www.test2.com
I want to log on to test1 and then log on to test2. how can I write this?

I have some test code as follows:
When logging on to test1, if the logon is successful, run the following command:
$ Fp = pfsockopen ("www.test2.com", 80, $ errorno, $ errormsh); // connect to the server
If ($ fp ){
$ Out = "GET/login. php? Acc = ". $ acc." & psw = ". $ psw." HTTP/1.1 \ r \ n ";
$ Out. = "Host: www.test2.com \ r \ n ";
$ Out. = "Connection: Close \ r \ n ";

Fputs ($ fp, $ out );
Fclose ($ fp );
}


Then the login. php file in www.test2.com
$acc = $_GET['acc'];
$psw = $_GET['psw'];
$_SESSION['acc'] = $acc;
$_SESSION['psw'] = $psw;


Then I access www.test2.com
The session is empty.
Please advise! PHP session cross-origin? Login? Share:
------ Solution --------------------
If single sign-on is so easy, isn't all SSO service providers shut down?

Session uses the sessionid carried by cookies for uniqueness identification
Normal cookies cannot be cross-origin. Although some technical means can be used to implement cross-origin cookie, this is, after all, a rule vulnerability is exploited and is not desirable.

We know that if a website has an invalid cookie, a cookie file with the domain name will be left in the browser buffer at the end of browsing. When you access the website again, the last saved cookie will be sent back.

Therefore, when you access www.test1.com, try to keep a cookie file www.test2.com.
Then you can implement synchronization when accessing www.test2.com.

How to stay? Use iframe or js to access a specific file of www.test2.com on the page returned by www.test1.com

Of course, this is only the basis, and more issues need to be considered.


------ Solution --------------------
As long as www.test2.com/login.php is called on the page returned by www.test1.com
That is to say, when you access www.test1.com, you also access www.test2.com.
In this way, the cookie www.test1.com is used to access www.test1.com.
Cookie for www.test2.com for accessing www.test2.com

------ Solution --------------------
"Called on the page returned by www.test1.com" means that the request is sent from the browser so that the returned cookie will be saved by the browser.

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.