|
|
 |
|
Lesson 8: Let's bake some cookies! |
|
Lesson 8: Let's bake some cookies!
Summary:
Learn how to use cookies in your ASP pages.
Intro:
One of the problems on the net is that you seldom know whom your dealing with.
Is it a newbie on your site, a regular guest or your boss?
Wouldn't it be cool if you could display different pages to
each of them? Show an intro and a guide to the newbie. Cut the
crap and display what's new to the regular, and show
of all the bells and whistles to the boss (you boss
will be impressed over that 10MB flash animation,
and don't bother to tell him that not everyone is
visiting your site from your local 100Mb network).
With cookies you can tag them, and know if they been
on your site before etc.
What are cookies?
Cookies are small pieces of information that you can send
to a visitors browser, and the browser will keep this information.
And it will reveal this info, upon request. But be aware of that
cookies are pretty insecure. Never store critical or sensitive info
in a cookie. And not all browsers support cookies (and some
people are so paranoid that they have turned cookies off).
Let's start baking!
Ok, let's bake a cookie. Check these lines:
<%
Response.Cookies("Name")="Bill Gates"
Response.Cookies("Name").Expires="Jan 1, 2010"
%>
What we do is setting a cookie called Name,
with the value Bill Gates. The expiration date
is set to 1st January 2010. If you don't set an exp. date,
the cookie will expire when the user closes the browser.
To read this value we use this code:
<%
Request.Cookies("Name")
%>
The above code will return the value Bill Gates.
Lets use this in a script:
Page1: Ask name, ask.asp
<HTML>
<BODY>
<FORM ACTION="bake.asp" METHOD="POST">
What's your name stranger?
<INPUT TYPE="TEXT" NAME="name">
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</BODY>
</HTML>
Page2: bake cookie, bake.asp
<%
Response.Cookies("Name")= Request.Form("name")
Response.Cookies("Name").Expires="Jan 1, 2010"
%>
<HTML>
<BODY>
Cookie is baked.
</BODY>
</HTML>
Page3: Read cookie, read.asp
<%
Name = Request.Cookies("Name")
%>
<HTML>
<BODY>
Hi there <%=name%>!
</BODY>
</HTML>
View this code by clicking here first (page1 & page 2), close the window,
and click here (page3). Download code: lesson8.zip.
Cool or what!?
Now I have given you a taste of cookies, so wipe
of those cookie crumbs from the edge of your mouth,
and start baking your own.
Where to go next:
Take a look at our lessons page
to get an overview of all our lessons.
| |
|
|
 |
|