Setting a cookie question

S

Steve Grosz

I'm trying to find a way to set a cookie if it doesn't
exist.

I know with ColdFusion there are some good programming
controls and commands, but not sure about ASP.

Would something like this work?

<%
If Not Exists("beenhere")
Response.Cookies("beenhere") = "donethis"
Response.Cookies("beenhere").Expires = "July 31, 2004"
%>

What I'm trying to do is check to see if the cookie
exists, if not, set it.

I also know that with ColdFusion you don't have to
specify a actual end date, you can do it in number of
days, can you do that with ASP?

Thanks,
Steve
 
J

Jim Buyens

Typically, you would use code like:

If Request.Cookies("beenhere") = "" Then
Response.Cookies("beenhere") = "donethis"
Response.Cookies("beenhere").Expires = now() + 60
End If

The Response.Cookies collection contains cookies that the
server sends the browser.

The Request.Cookies collection contains cookies that the
browser sends back to the server.

The statement
Response.Cookies("beenhere").Expires = now() + 60
sets the expiration date of the "beenhere" cookie 60 days
in the future.

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------
 
S

Steve Grosz

I'm not sure why its not being set then. I have the following code as
the first line on the default.htm page:

<%
If Request.Cookies("beenhere") = "" Then
Response.Cookies("beenhere") = "donethis"
Response.Cookies("beenhere").Expires = now() + 60
End If
%>

If after posting the page, and then opening my browser and refreshing the page, the cookie should be set on my machine. If I search for the cookie, which I named Visitor instead of "beenhere", I don't find anything with Visitor on my machine.

Why is that? Do I need to define something in the header file? I have a Javascript line as follows:
<script language="JavaScript" type="text/javascript" src="http://www.scootervilleusa.com/Menu/menu_scr.js">


Would that be messing things up?

Steve
 
R

Ronx

In your cookies folder (C:\documents and settings\username\cookies in WinXP)
there are files with names similar to [email protected]
These files contain all the cookies from the domain. Open the file
corresponding to your domain, and you should find the cookie.

Ron
--
Reply only to group - emails will be deleted unread.


Steve Grosz said:
I'm not sure why its not being set then. I have the following code as
the first line on the default.htm page:

<%
If Request.Cookies("beenhere") = "" Then
Response.Cookies("beenhere") = "donethis"
Response.Cookies("beenhere").Expires = now() + 60
End If
%>

If after posting the page, and then opening my browser and refreshing the
page, the cookie should be set on my machine. If I search for the cookie,
which I named Visitor instead of "beenhere", I don't find anything with
Visitor on my machine.
 
J

Jim Buyens

Curious, because this code definitely works on my system.

I'd say the next thing to check are your Internet security settings.
Make sure you allow cookies for the Web server in question.

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------
 
S

Steve Grosz

So, here's the code I have on the main page (or at least the cookie part):

<body>
<%
If Request.Cookies("Unique") = "" Then
Response.Cookies("Unique") = "visited"
Response.Cookies("Unique").Expires = now() + 30
End If
%>
<p align="center">
<img src="images/logo1.jpg">
</p>

And it still doesn't seem to be setting it, even with the browser
settings set as low as they'll go. Can you see if the page is setting
it for you? www.scootervilleusa.com

Thanks!

Steve
 
T

Thomas A. Rowe

1. In order to use ASP on a page, the page must have .asp extension.
2. You need to use JavaScript to create the cookie, not VBScript, then you can use ASP to read the
cookie.

--
==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
WEBMASTER Resources(tm)

FrontPage Resources, WebCircle, MS KB Quick Links, etc.
==============================================


Steve Grosz said:
So, here's the code I have on the main page (or at least the cookie part):

<body>
<%
If Request.Cookies("Unique") = "" Then
Response.Cookies("Unique") = "visited"
Response.Cookies("Unique").Expires = now() + 30
End If
%>
<p align="center">
<img src="images/logo1.jpg">
</p>

And it still doesn't seem to be setting it, even with the browser
settings set as low as they'll go. Can you see if the page is setting
it for you? www.scootervilleusa.com

Thanks!

Steve
should be set on my machine. If I search for the cookie, which I named Visitor instead of
"beenhere", I don't find anything with Visitor on my machine.
 
J

Jim Buyens

Yes, the page sets the cookie for me. I discovered this by:

1. Choosing Internet Options from the Tool menu.
2. On the General Tab, under Temporary Internet
Files, clicking Settings
3. In the Settings dialog box, clicking View Files.
4. In the resulting Windows Explorer window, clicking
the Last Modified column heading until that column
appears in descending order.
5. Searching visually from the top of the list.

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------
 
J

Jim Buyens

-----Original Message-----
1. In order to use ASP on a page, the page must
have .asp extension.
True.

2. You need to use JavaScript to create the cookie,
not VBScript, then you can use ASP to read the
cookie.

False. You can easily set cookies using VBScript
at the server. Anything you set in, say,
Response.Cookies("oatmeal") will come back in
Request.Cookies("oatmeal").

Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------
 
T

Thomas A. Rowe

Jim,

You can't run VBScript on a .htm(l) page, which is what I was really trying to say, instead
JavaScript would need to be use to set the cookie the .htm(l) page, then on an ASP page you could
read the cookie set from a .htm(l) page.

--
==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
WEBMASTER Resources(tm)

FrontPage Resources, WebCircle, MS KB Quick Links, etc.
==============================================
 
Top