Using Cookies to see if our Visitor has been Before

<% If Request.Cookies("BeenHereBefore") = "True" Then
sMessage = "You've been to this page before!"
Else
sMessage = "Hi, Hope you enjoy this page and come back again!"
Response.Cookies("BeenHereBefore") = "True"
Response.Cookies("BeenHereBefore").Expires=Now+60
End If

Response.Write(sMessage)
%>

When our visitor arrives at our page we check to see If our cookie called BeenHereBefore exists and if it has the value "True". If it does then we set the value of the sMessage variable to "You've been to this page before!". If the cookie doesn't exist and doesn't hold the value "True" then we set sMessage to "Hi, Hope you enjoy this page and come back again!". We also write the cookie BeenHereBefore to the visitors machine. Our cookie also has an attribute, in this case a date when the cookie will expire. It's 60 days from now i.e. 'Now + 60'.

If you do not set the Expires attribute then the cookie will expire when the user closes their browser.

Here's our code in practice:
Hi, Hope you enjoy this page and come back again!

If you get the message "Hi, Hope you enjoy this page and come back again!" click refresh on your browser and see what happens.

Read more on Reading and writing cookies

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Get your best asp web hosting provider now and save 25%