ASP Primer: Making a Request
Building the Right Environment to Support AI, Machine Learning and Deep Learning
Well, I'm sure you figured out from the last lesson that the Response object's primary goal is to send stuff out, whether it be to the browser or to the user's computer. This week we are going to start working with the Request object. Its goal is pretty much the exact opposite of the Response object. Instead of sending stuff out it's gathering stuff.
Request.Cookies: Getting Stuff Out of a Cookie
As promised, we will continue the practical example from the last lesson by retrieving information from our cookie and displaying the number of days since the person's last visit. I'll give you the code and then we'll take a look at how it works:
<% Option Explicit %>
<% Dim dtmLastVisited %>
<% dtmLastVisited =
<% If dtmLastVisited <> Date( )
And dtmLastVisited <> "" then %>
<% Response.Write "Welcome back! It's been " &
DateDiff("d",dtmLastVisited,Date( )) &
" days since you last visited!" %>
<% End If %>
= Date( ) %>
<% Response.Cookies("MyCookie").Expires =
DateAdd("m",6,Date( )) %>
<% Response.Cookies("MyCookie").Domain =
Alright, here we go. The first thing that you will notice is that we create a variable called "dtmLastVisited" to store the last date the user visited.
Next we will then set our new variable equal to the date the user last visited by retrieving the information from the cookie. It is done by using Request.Cookies with the cookie's name "MyCookie" and the item that we want to retrieve "last_visit".
The next line is an If .. Then statement. We are using the If .. Then to check two different scenarios. The first is to see if the last visit date is today's date. If it is, there is no reason to print out our welcome. The second is to see if there was a cookie at all. If there was no cookie present then the Request.Cookies for "last"visit" will return nothing (two double quotes together "" indicates an empty string which means there is nothing there). If we didn't check to see if "dtmLastVisited" was blank and it was, we would get an error in our DateDiff function.
If the two criteria are met, we will print out our welcome message. Using Response.Write we will piece together our message using the ampersand "&" and the DateDiff function. If you remember back from our discussion on functions, DateDiff calculates the number of minutes, days, months, years or whatever you like between two different dates/times. In our case, we will be looking for the number of days, which is designated by the "d" in the DateDiff function.
Once we have printed out our welcome message, we need to close out the If .. Then statement with an End If.
Lastly, we have the code to write our cookie that comes from the last lesson. We want to write the cookie after our welcome message otherwise our If .. Then statement will always fail because the cookie's date would always be today's date.
Keep in mind, this code will only execute the welcome message one time when a person returns to your site. After that initial return to the site the cookie will be reset to today's date and the If .. Then will not execute anymore that day.
That's all there is to getting a value back out of a cookie. It's actually easier than putting stuff into a cookie.
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...