Home  >  Article  >  Backend Development  >  Implement cross-domain cookie transfer to http//www.phprecord.com_PHP tutorial

Implement cross-domain cookie transfer to http//www.phprecord.com_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:28:22966browse

Author: Christopher Kings-Lynne Translation: limodou Cookie is really a great invention, it allows web developers to retain the logged-in status of their users. However, problems can arise when your site or network has more than one domain name. According to the Cookie specification, a cookie can only be used for one domain name and cannot be sent to other domain names. Therefore, if a cookie is set in the browser for one domain name, the cookie will not be valid for other domain names. If you want your users to log in from one of your sites and also log in from other domain names, this can be a big problem. My solution will use the following general framework: A pre-built script will be used to accept the sessionid number passed through GET or COOKIE. It will prefer GET variables over COOKIE. So, whenever we need to reference cross-domain names, we send the sessionid as a URL parameter. Modify the Apache configuration to rewrite all cross-domain cookies. The reason for this will become clear in a moment. Use the variable any time a cross-domain reference occurs. Step 1: Create a preset script. Add the following code to the preset script (or appear in the function before all scripts). Once this code runs, a global sessionid variable will be available to the script. It will save the sessionid value in the user's cookie, or the sessionid value sent through a GET request. Step 2: Use variables for all cross-domain name references Create a global configuration file to store the basic reference form of domain names that can be switched. For example, if we have domain1.com and domain2.com, the following is set: Now, if in the code we do: You will produce the following output: Click here to contact us. Here the sessionid has been Inserted into the URL. At this point, you may be thinking, "This may open a subdirectory named horizontal line, sessionid, horizontal line on the web server?!?!?". However, the following steps will provide a necessary trick to make it work! Step 3: Configure Apache Now, the remaining steps are to configure apache to rewrite this URL: http://www.domain2.com/- 66543afe6543asdf6asd-/contact/ becomes like this: http://www.domain2.com/contact/?sessionid=66543afe6543asdf6asd And this URL: http://www.domain2.com/-66543afe6543asdf6asd-/contact/?email=yes It becomes like this: http://www.domain2.com/contact/?email=yes&sessionid=66543afe6543asdf6asd To achieve this, simply configure two virtual servers, as domain1 and domain2, as follows: DocumentRoot /usr/local /www/domain1 ServerName www.domain1.com RewriteEngine on RewriteRule ^/-(.*)-(.*?.*)$ $2&sessionid=$1 [L,R,QSA] RewriteRule ^/-(.*)-( .*)$ $2?sessionid=$1 [L,R,QSA] DocumentRoot /usr/local/www/domain2 ServerName www.domain2.com RewriteEngine on RewriteRule ^/-(.*)-(. *?.*)$ $2&sessionid=$1 [L,R,QSA] RewriteRule ^/-(.*)-(.*)$ $2?sessionid=$1 [L,R,QSA] These rewrites The rules implement the above two URL rewriting requirements. Conclusion Cross-domain cookies can be implemented in a simple way by using variables combined with Apache's rewrite functionality. If you want to maintain such a system, whenever you link cross-domain names, nothing is used except using domain name variables! Links within domain names do not need to be modified as cookies will work fine. If you are interested in seeing the system in action in a production network, please visit http://www.familyhealth.com.au/. Move your mouse over some cross-domain links and see how they are rewritten when you click on them. Perhaps the only problem with using this technology is that it cannot delete cookies for all domain names in the user's browser. Reprinted from: PHPBuilder.com

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531774.htmlTechArticleAuthor: Christopher Kings-Lynne Translation: limodou Cookie is really a great invention, it allows web developers to keep their The user's login status. However, when your site or network has...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn