Introduction to Cookies
First, let's make a simple introduction to cookies, explaining how to use ASP to maintain cookies.
A cookie is a small file stored on a client computer, which means that whenever a user accesses your site, you can secretly place a file containing the information on its hard disk. This file can contain almost any information you intend to set, including user information, site status, and so on. In this case, there is a potential danger: this information may be read by hackers. An effective way to prevent this problem is that cookies can only be accessed by the domain in which it was created. This means that, for example, ytu.edu.cn can only access cookies created by ytu.edu.cn. Usually, there is no problem with this, but what if you need two different sites on two different websites to share user information stored in cookies? Of course, you can choose to copy the user letter, but if you need a user can only register on one site, and from the east to become another site registered users? Or, two sites share a user database and require users to log in automatically? This is the best solution for sharing cookies across domains.
Here, first look at some of the ASP processing cookies code to facilitate reference later.
'创建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 code above creates a cookie and sets the properties for the cookie: domain, expiration time, and other values stored in the cookie. Here, the Strusename,strpassword is a variable that was set up somewhere before. Then, read through the cookie in the following statement.
'读取Cookie
datExpDate=Request.Cookies("MyCookie")
strDomaln=Request.Cookies("MyCookle").Domain
strUsername=Request.Cookies("MyCookle")("Username")
strPassword=Request.Cookies("MyCookie")("Password")
More detailed information, you can refer to the ASP data.
Realize
The trick to simply share cookies is to redirect them, and the general process is:
1. A user clicks on the sitea.com.
2. If the user does not have a sitea.com cookie, redirect the user to the siteb.com.
3. If the user has a siteb.com cookie, the user is redirected back to the sitea.com with a special flag (explained below), otherwise, the user is redirected to the sitea.com only.
4. Create cookies in sitea.com.
It looks very simple: sitea.com and siteb.com share the same user settings, so if the user has a siteb.com cookie (already registered), sitea.com can also read the cookie and provide the properties allowed by the cookie. This allows access to sitea.com users as if they were visiting siteb.com.
This check should be implemented in a cookies.inc in the file contained in sitea.com. Let's take a look at this piece of code:
l—1
'SiteA.com"检查cookie
If Request.Querystring("Checked")<>"True" then
If not Request.Cookies("SiteA_Cookie").Haskeys then
'重走向到siteB.com
Response.Redlrect("http://www.siteB.com/cookie.asp")
End if
End if
If the user has a sitea.com cookie, nothing needs to be done; The first if statement is used to eliminate an infinite loop. Let's take a look at the cookie.asp file on siteb.com to get a further understanding.
1—2
'SiteB.com
'检查cookie
If not Request.Cookies("SlteB_Cookle").Haskeys then
'重定向到 siteA.com
Response.Redirect("http://www.siteA.com/index.asp"&"?checked=True")
Else
'获取username
strUsername=Request.Cookies("SiteB_Cookie")("Username")
'将用户连同一个特殊的标志返回到siteA.com
Response.Redlrect("http://www.siteA.com/index.asp"&"?checked=True"&"identrfer="&strUsername)
End if
If the user still does not have a cookie on the 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 statement. Otherwise, send the user back to siteb.com and exit the loop
However, if the user has a siteb.com cookie, we need to send the user back to sitea.com and tell Sitea.com. To do this, we attach a unique logo to the database, username. So, we extend the code in sitea.com.
l—3
'SiteA.com
...
...
'检查标志
If Request.Querystring("identifier")<>"" then
strUsername=Request.Querystring("identifier")
'记录到数据库
Response.Cookies("siteA_Cookie").Expires=Date+365
Response.Cookies("SiteA_Cookie").Domain="siteA.com"
Response.Cookies("siteA_Cookie")("Username")=strUsername
End if