Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-24-2003, 12:53 PM   #1 (permalink)
Sarlok
Registered User
 
Join Date: Feb 2003
Location: Nowhere special
Posts: 10
Sarlok is on a distinguished road
Send a message via ICQ to Sarlok
Setting Cookies

This example creates a cookie that counts the number of times each person has visited your site. It will also expire in 10 days from thier last visit.

Here's the code:
Code:
<%

response.cookies("MyCookie").Expires = date + 10 
num=request.cookies("MyCookie")
if num = "" then
      response.cookies("MyCookie") = 1
else
      response.cookies("MyCookie") = num + 1
end if
%>
First things first. The <% and %> indicate where the code starts and ends, so the server knows what to do with what, and where.

response.cookies("MyCookie").Expires = date + 10
Next, create the cookie, and make it expire 10 days from now. Also, we will call the cookie MyCookie.

num=request.cookies("MyCookie")
This line asks to open MyCookie, and then fill the value "num" with whaterever's inside.

if num = "" then
Now, if this is the persons first visit to the page, the cookie will be empty (Oh No!). Now, we can't have that. Otherwise, there would be no point.
So, this IF checks to see if it is empty

response.cookies("MyCookie") = 1
If the cookie was empty, then we get to this line. This will put the number "1" in the cookie.

response.cookies("MyCookie") = num + 1
If it wasn't empty (else) then it goes to this line, which will incriment (increase, add 1 to it, whichever you prefer) the number it got from the cookie by 1, and then save the new number to the cookie after.

There you go, you know how to get information from, a cookie and put some in one. Now, see the full code example below to see some other things you can do with a cookie. It's fairly straight forward.
Except for maybe this, which I'll explain here:

<%response.write(num)%>
Put that in your page, wherever you want the cookies value displayed, and it will do so.

- Sarlok

Code:
<%
response.cookies("MyCookie").Expires = date + 10 
num=request.cookies("MyCookie")
if num = "" then
      response.cookies("MyCookie") = 1
else
      response.cookies("MyCookie") = num + 1
end if
%>

<html>
<body>
<%if num="" then%>
If the cookie was empty, we will see this lovely welcome message.<br>
Welcome! This is the first time you have visited my cookie page.<br>
<%else%>
If it wasn't empty, we will see this.<br>
Welcome back. You've been here <%response.write(num)%> time(s).<br>
<%end if%>
</body>
</html>
Sarlok is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting the path for python scripting Intensegutwound All Other Coding Languages 7 11-23-2004 06:32 AM
setting the focus on a windows form with c# sde MS Technologies ( ASP, VB, C#, .NET ) 0 07-28-2003 06:14 AM
Help with setting up mySQL and PHP Ilya020 PHP 11 03-19-2003 05:10 AM


All times are GMT -8. The time now is 10:10 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting