How to use ASP to check whether the Browser allows cookies
Author: Dong Yun Dong
Work Unit: Huangpu customs Technical Department Engineer
Address: Customs Building, Zhicheng Avenue, Guangzhou Economic and Technological Development Zone
Phone number: 82130556-
Zip code: 510730
Email: dy168@163.net
Abstract: This article points out some mistakes in detecting browser cookies using ASP, describes the causes of the errors, and provides
Solution andSource code
Keywords: browser, Cookie, information record area
I. Preface
It is not difficult to check whether the Browser allows the cookie function. ManyArticleWe have discussed this issue. However
There are often many problems in some articles, making readers easy to confuse, and even some of the methods provided in some articles are simply
Incorrect. This article will explain the difficulties step by step, and provide practical ASPCode.
Ii. Question proposal
Although the current browser basically supports the cookie function, you can disable the cookie function by setting,
For a web server that wants to record some information to the browser, you must be clear about the user browsing
The only way is to test whether your browser supports cookies. This question
The question seems very simple, as described in many articles, I will first give an ASP code, it seems to be light
Complete the task easily.
Cookie_test.asp
<% @ Language = "VBScript" %>
<%
Option explicit
dim cookievalueset
dim cookievalueread
cookievalueset = "true ""
cookievalueread =" false "
response. cookies ("cookie_test") = cookievalueset
response. cookies ("cookie_test "). expires = date + 1
cookievalueread = request. cookies ("cookie_test")
If cookievalueread = cookievalueset then
response. write "the browser supports cookies"
else
response. write "the browser does not support cookies"
end if
%>
In fact, when the Browser allows or disables the cookie function, the above Code will all receive results that the browser supports cookies
. Why is this? First, let's take a look at the principles of cookies. The browser accesses the Web server according to the HTTP protocol. A full HTTP request is requested by the browser, and the Web Service
server responds to the request, in this process, the browser will provide an information record area to store some special information.
This information record area can be accessed by both the browser and the web server, the cookie is saved in the
record. In this information record area, the web server can write cookies to record some information, and access can be performed in the
future until the life cycle of the cookie ends. In ASP code, response. Cookies and
request. Cookies are used to access cookies.
the problem is that, after a cookie is written to a browser based on the HTTP protocol, its content actually exists in the store until the page ends.
, the content is written to the information record area provided by the browser. Write
the cookie on the same page and read it immediately. The Temporary Information in the cache is read, not the real
cookie content in the information record area, therefore, in the above Code, even if the browser disables the cookie function, it still gets the result of successfully writing the cookie
.
Iii. Problem Solving
There are two ways to solve this problem. The first method is to use two page tokens for Cookie
Write and read, write the cookie in the first Shard, and then switch to the second Shard to read and judge.
Get the correct results easily. However, it is obvious that the method of accessing the two pages is troublesome to manage the web server,
This is the best solution without worrying about this problem.
The second method is to use the Redirect statement in ASP to solve the problem. The author provides the ASP source code as follows. Bytes
The browser accesses cookie. asp, which contains some code to determine whether the cookie check has been performed. If yes,
The check result is displayed. If no, the system automatically redirects to cookie_detect.asp labels to test the cookie
Function and return the result to Cookie. asp. It is worth noting that we cannot use ASP cookies and sessions,
Because they all use the information record area mentioned above to store information, when the cookie function is disabled, it cannot cross-page
. Therefore, I use querysring to pass parameters.
Iv. Source Code
Cookie. asp
<% @ Language = "VBScript" %>
<%
Option explicit
Response. Buffer = true
Dim bcookies 't/ F indicating cookies enabled.
Bcookies = request. querystring ("cookies ")
Select case lcase (bcookies)
Case "true", "false"
'We got a valid response.
Bcookies = cbool (bcookies)
Case else
Response. Cookies ("cookie_test") = "true"
Response. Redirect "cookie_detect.asp"
End select
%>
<HTML>
<Head>
<Title> check whether the Browser allows the cookie function </title>
</Head>
Cookies of this Browser: <% = bcookies %>
</Body>
</Html>
Cookie_detect.asp
<% @ Language = "VBScript" %>
<%
Option explicit
response. buffer = true
dim bcookiesenabled
bcookiesenabled = request. cookies ("cookie_test")
bcookiesenabled = (bcookiesenabled = "true")
response. redirect "cookie. ASP "&"? Cookies = "& bcookiesenabled
%>