Home >Backend Development >PHP Tutorial >Implement cross-domain cookie transfer to http//www.phprecord.com_PHP tutorial
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: