Cookie introduction
First, we give a brief introduction to Cookies and explain how to use ASP to maintain cookies.
A cookie is a small file stored on the client's computer, which means that every time a user visits your site, you can secretly place a file containing relevant information on his or her hard drive. This file can contain almost any information you want to set up, including user information, site status, and more. In this case, there is a potential danger: the information may be read by hackers. To prevent this problem from happening, an effective way is that the cookie can only be accessed by the domain that created it. This means: for example, ytu.edu.cn can only access cookies created by ytu.edu.cn. Generally speaking, this is not a problem; but what if you need to share user information stored in cookies with two different sites on two different domains? Of course, you can choose to copy the user information, but if you need to Can you only register on one site and become a registered user of another site? Or, what if the two sites share a user database and require users to log in automatically? At this time, sharing cookies across domains is the best solution plan.
Here, let's first look at some ASP cookie processing code for easy reference in the future.
'Create Cookie
Response.Cookies(MyCookie).Expires=Date+365
Response.Cookies(MyCookle).Domain=mydomaln.com
Response.Cookies(MyCookle)(Username)=strUsername
Response.Cookies(MyCookle)(Password)=strPassword
Reading and writing cookies is very simple. The above code creates a cookie and sets properties for the cookie: domain, expiration time, and other values stored in the cookie. Here, strUsename, strPassword are variables set somewhere before. Then, read in the cookie through the following statement.
'Read Cookie
datExpDate=Request.Cookies(MyCookie)
strDomaln=Request.Cookies(MyCookle).Domain
strUsername=Request.Cookies(MyCookle)(Username)
strPassword=Request.Cookies(MyCookie)(Password)
For more detailed information, please refer to the ASP information.
accomplish
The trick to simply sharing cookies is redirection, the general process is:
1. A user clicks on siteA.com.
2. If the user does not have a cookie for siteA.com, redirect the user to siteB.com.
3. If the user has a cookie for siteB.com, redirect the user back to siteA.com along with a special flag (explained below), otherwise, just redirect the user to siteA.com.
4. Create a cookie at siteA.com.
It seems simple, but analyze it carefully: siteA.com and siteB.com share the same user settings, so if the user has a cookie for siteB.com (already registered), siteA.com can also read the cookie and provide cookie permissions characteristics. In this way, users who visit siteA.com will appear to have visited siteB.com.
This check should be implemented in a cookies.inc file contained in siteA.com. Let's take a look at this code:
l-1
'SiteA.com checks cookies
If Request.Querystring(Checked)<>True then
If not Request.Cookies(SiteA_Cookie).Haskeys then
'Redirect to siteB.com
Response.Redlrect(http://www.siteB.com/cookie.asp)
End if
End if
If the user has a cookie for siteA.com, nothing needs to be done; the first if statement is used to eliminate the infinite loop. Let's take a look at the cookie.asp file on siteB.com to gain further understanding.
1-2
'SiteB.com
'Check cookies
If not Request.Cookies(SlteB_Cookle).Haskeys then
'Redirect to siteA.com
Response.Redirect(http://www.siteA.com/index.asp&?checked=True)
Else
'Get username
strUsername=Request.Cookies(SiteB_Cookie)(Username)
'Return the user to siteA.com with a special flag
Response.Redlrect(http://www.siteA.com/index.asp&?checked=True&identrfer=&strUsername)
End if
If the user still does not have a cookie on siteB.com, send him back to siteA.com and let the application know that you have checked the cookie by providing a parameter called checkd in the query. Otherwise, send the user back to siteB.com and exit the loop.