How do you use a cookie in ASP Classic right after it is created on the page?

(Updated: 6:33 PM - February 25, 2024)
Live Lessons Example - ASP Classic How to get the value of a cookie when it is first created
[How cookies work]
For the example of this script, we have a cookie created when the visitor loads the page.
The cookie is then used to create an entry in the database that checks every time the page is loaded to see if the cookie exists and if it matches the cookie data we have stored in the database.
The original way I was doing this was by doubling the INCLUDE FILE for the Hits.asp page. This worked, but was not the correct way.
While working on the Radio site, I ran into this issue once again, but this time, instead of doing it the easy way, which was doubling the Hits.asp include file.
I decided to try something else:
    [How we do this.].
  1. Add the cookie Creation script to the top of the of the page.
  2. Then we place the Database hits counter at the bottom of the page, in the FOOTER.

  3. Since the Cookie is created in the page header, it is ready to be read in the page body.
    See the demo script for an example.
First, we place our header code at the top of the page.
[Creating Variables]
CFFCS | CarrzSynEdit: | ASP/VBScript

<%
getType = request.QueryString("Type")
strID = request.QueryString("ID")
getIt = 8
getIP = "192.168.2.12"
Response.Cookies("MyCookie"&getType&"-"&getIt&"")("Value") = getIP&"("&strID&"-"&getIt&"-"&time&")"
getHash = request.Cookies("MyCookie"&getType&"-"&getIt&"")("Value")
%>

Next, we have our HTML script for our page.
[Load.asp]
CFFCS | CarrzSynEdit: | ASP/VBScript

<html lang="en" dir="ltr">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      </head>
      <body> 
<%
' Since the Cookie was created at the top of our page before the <HTML> was created, 

' We will be able to read the Cookie immediately after its creation.

' Here, we check to see if the Cookie exists. If it does not, then we 

if getHash<>"" then%>
Add your Database script here for the Page HIT counter.
OR
Add in whatever you want the user to see if a cookie is installed on their system.
<%end if%>
<!-- This will go in the Footer if this is part of a Page Hits Counter -->
<%=getHash%>
</body>
</html>