Click to See Complete Forum and Search --> : Cookie Check


mcslemon
05-06-2003, 03:31 AM
Hello.

I'm running a survey. When the person looks at the survey, the page writes a cookie to say they've completed the survey. Thing is, once they have done it they can click back and do it again. I need to stop this. I've used the following code to check for the cookie but I'm not sure if its correct.

<%
Response.Buffer = True

Dim strCookieName

strCookieName = CStr("SurveyCompleted")

if strCookieName <> "SurveyCompleted" then
%>

at the end of the code there is:

<%
else

response.redirect="webaddress"

end if

%>

This part follows if the cookie isn't there. Basically creating the cookie if they've not done the survey and allowing them to continue.

<%
Response.Cookies("SurveyCompleted") = CStr(Now)
Response.Cookies("SurveyCompleted").Expires = "Jun 1, 2003"
%>
*******survey continues***********

Is this code correct?

Neil.

cmelnick
05-06-2003, 03:07 PM
Your code right now only writes the cookie to a response, it doesn't read it from the request.

The other thing you need to note is that if you want to write a cookie to the user's browser, you MUST set the Response.Cookies object BEFORE you write anything else to that web page. The cookies MUST be at the very top of the content written to the page.

After your visitor completes the survey, use:


' Using the Response object sets the cookies
Response.Cookies("SurveyCompleted") = Now()
Response.Cookies("SurveyCompleted").Expires = "Jun 1, 2003"


But at the TOP of your survey page, you need to have:


' Using the Request object gets the cookies
If Request.Cookies("SurveyCompleted") = "" Then
' User has not completed the survey
Else
Response.Write "You already completed the survey on " & _
Request.Cookies("SurveyCompleted")
End If