Jump to content

Browser Dose Not Support Cookies?


dhc000531
 Share

Recommended Posts

Hi, I put following code before <html> to test cookies:<%dim numvisitsresponse.cookies("NumVisits").Expires=date +365numvisits=request.cookies("NumVists")Response.Write("This is a test on initial times of Visits:" &numvisits)If numvisits=" " then response.cookies("NumVisits")=1 response.write("Welcome! This is your first time to visite this page") else response.cookies("NumVists") = numvisits +1 Response.Write("Welcome! you have visited this") response.write("Web page" &numvisits) If numvisits = 1 then response.write("time before!") else response.write("times before!") end Ifend If%><html>.....but webpage could not display anything. It appears as empty blank. My Asp was successfully install and function well.Does that means my browsers does not support Cookies? What happen if the browser do not support Cookies?many thanks!

Link to comment
Share on other sites

you may use a browser that denies coockies or has some filters active...IE doesn't have this, but other browsers, like Opera, Maxthon..., do!

Link to comment
Share on other sites

You can't set and retrieve a cookie on the same page, this won't work for any browser. What a cookie actually is is an http header. The headers come before the actual page and tell the browser what character encoding, or mime type, or other properties of the page. One of the headers can be a cookie asking the browser to save some information. But the key point is that headers are delivered along with the page, but come before. So when you have the code to set the value in the cookie, it creates a header for that cookie to send to the browser, but it hasn't actually sent it yet. The rest of the script needs to finish executing, so the cookie only gets sent when the script ends and the page gets sent to the browser. And by the time the script ends, you have already done your check to see if the cookie is there, and obviously it's not because it's still waiting to be sent.So you need to create one page that sets the cookie, and give yourself a link to a second page that checks the cookie. If you are doing this for a login, you will want to have the login page submit to some in-between page ("thank you for logging in, you will be redirected") that sets the cookie and then redirects the user to the menu or whatever.

Link to comment
Share on other sites

dhc000531,Nothing is wrong with your server, just your code. You've misspelled your cookie. Observe:<%dim numvisitsresponse.cookies("NumVisits").Expires=date +365numvisits=request.cookies("NumVists")Response.Write("This is a test on initial times of Visits:" &numvisits)If numvisits=" " thenresponse.cookies("NumVisits")=1response.write("Welcome! This is your first time to visite this page")elseresponse.cookies("NumVists") = numvisits +1Response.Write("Welcome! you have visited this")response.write("Web page" &numvisits)If numvisits = 1 thenresponse.write("time before!")else response.write("times before!")end Ifend If%>You're using "NumVists" when you should be using "NumVisits".Here is a re-write of your code:

<%Option Explicitdim NumVisitsResponse.Cookies("NumVisits").Expires = DateAdd("m", 12, Now())NumVisits = Request.Cookies("NumVisits")Response.Write "This is a test on initial times of Visits: " & numvisitsIf cstr(numvisits) = "" then    response.cookies("NumVisits") = 1    response.write("Welcome! This is your first time to visite this page")else    response.cookies("NumVisits") = NumVisits + 1    Response.Write("Welcome! you have visited this ")    response.write("web page " & NumVisits)    If numvisits = 1 then        response.write(" time before!")    else         response.write(" times before!")    end Ifend If%>

See a demonstration of the above code is available here:http://www.fstdt.com/scripts/cookie/visits.aspJustsomeguy,

You can't set and retrieve a cookie on the same page, this won't work for any browser. What a cookie actually is is an http header.  The headers come before the actual page and tell the browser what character encoding, or mime type, or other properties of the page.
That's not quite right, I set and retrieve cookies on the same page all the time.Here's an example of setting and retrieving cookies on the same page:
<% Option Explicit %><html><head>	<title>Cookies!</title></head><body><p>This is an example of a script that sets a cookie, and retrives it on the same page. The cookie we'll be creating will be called <code>Bunny</code>. The current value of <code>Bunny</code> is:<blockquote>	<hr>	<%=Server.HTMLEncode(Request.Cookies("Bunny"))%>	<hr></blockquote></p><p><form action="default.asp" method="post">	Type a value into the textbox below. It will be used to set the <code>Bunny</code> cookie:<br>	<input name="txtCookie" type="Text" value=""><br>	<input type="Submit" value="Set Cookie"></form></p><%Dim strCookiestrCookie = Request("txtCookie")If cstr(strCookie) <> "" then    'Setting the cookie    Response.Cookies("Bunny") = strCookie    Response.Cookies("Bunny").Expires = DateAdd("m", 12, Now())%><p>Here is the new value of <code>Bunny</code>:<blockquote>	<hr>	<%=Server.HTMLEncode(Request.Cookies("Bunny"))%>	<hr></blockquote>Notice that the cookie was set and retrieved on the same page, even after the headers for the page had already been sent.</p><%End if%></body></html>

See a demonstration of that code here:http://www.fstdt.com/scripts/cookie/default.asp

Link to comment
Share on other sites

I don't think that's technically true. The cookie is a header, and when the browser makes a request to the server, it sends any cookies that go with that domain along with the request. So the server only receives cookies when the browser makes the original request, before page execution even begins. Before the first line of code executes, all cookies that are going to be sent already have been sent.I realize that the example shows the same value, but it's not reading that value from the HTTP cookie that the browser sent, it's reading the value from the Response.Cookies ASP collection. In fact, I'm pretty sure that even if the browser does not accept cookies or has them turned off, your example will still show the value coming from the cookie, because it is reading the value from the Response.Cookies collection, not from the client machine. (note: I just tested this) With cookies disabled, the value of the cookie still showed up as being there, even though my browser had not accepted the cookie. So if you use this method to test for the existence of a cookie after setting it, it will always come back as true.Even though this may work with ASP (because you are reading the Cookies collection and not the cookie itself), I think it's still important to understand how cookies work, both to help yourself debug problems with them and also so that if you go to another language your understanding doesn't change.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...