Cookies across domain cookies
Zhao Jindong
All Web developers will love the powerful nature and ease of use of cookies, which have a powerful role in tracking user information, building personalized, personalised web sites, and avoiding the expensive cost of using a database. However, cookies cannot be passed across domains, only those domains that create it can be accessed; here we discuss how to use ASP to break through this limitation.
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.
' Create cookies
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.
' Read cookies
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 ' Check cookies
If request.querystring ("Checked") <> "True" Then
If not Request.Cookies ("Sitea_cookie"). HasKeys Then
' Re moving to 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
' 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 with a special flag to 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
...
...
' Check mark
If request.querystring ("identifier") <> "" Then
Strusername=request.querystring ("identifier")
' Log to Database
Response.Cookies ("Sitea_cookie"). expires=date+365
Response.Cookies ("Sitea_cookie"). Domain= "Sitea.com"
Response.Cookies ("Sitea_cookie") ("Username") =strusername
End If
Finally, we return to sitea.com. Text