PHP uses session to synchronously log on to two different server websites

Source: Internet
Author: User
Tags php session
PHP uses session to synchronously log on to two different server websites. PHP session Cross-Origin login

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!


Reply to discussion (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.

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.
Excuse me, boss. I can't handle this.
Write the cookie creation code in the login. php file in www.test2.com.

setcookie("acc",$acc,time()+3600,'/');

In this way, when I access www.test2.com, the cookie will directly send the page
Then, when I access the service, I can add a cookie to determine whether or not it works?

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

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
How is it called on the returned page?
Index. php in www.test1.com
The method for accessing www.test2.com after successful logon is called!
The actual situation is as follows:
Use login. php in www.test1.com after successful logon,

$ 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 index. php page in www.test1.com will be displayed.
The cookie is written in login. php in www.test2.com.
Setcookie ("acc", $ acc, time () + 3600, "/"); // write a file, check whether the cookie is successfully accessed and written. $ fp = fopen ("test.txt", "w"); fwrite ($ fp, "acc :". $ _ COOKIE ['ACC ']); fclose ($ fp );


I just tested the example and found that the test.txt file exists, and there is data in it.
Then I access www.test2.com.
Var_export ($ _ COOKIE) is added to the page );
The data is not written by me. why?
This is not the boss. you just said, "it's called on the page returned by www.test1.com"

"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.

"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.

echo "";

I used this method after logging on.
The cookie does not contain the data I have written, and the files cannot be created. Am I too stupid?
Is this wrong? Another method should be used to ask the boss for advice!

Store sessions in "memory databases" such as databases or memcache separately, and retrieve all sessions from the session database when reading sessions from various websites.

Store sessions in "memory databases" such as databases or memcache separately, and retrieve all sessions from the session database when reading sessions from various websites.
Simple and clear. What are the advantages of the big version ..

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.