PDA

View Full Version : ASP Cookies...need a little help



RabBell
October 5th, 2004, 11:28 AM
Ok heres the problem

I have 3 pages. The first page has 2 buttons, one says buy tickets, the other says no tickets. Clicking on either one will send a parameter and the browser to the second page which creates a cookie and stores the parameter in there.

Now if I wanted to view this cookie in the second page I'd be able to see it but I don't. I want to see it in the third page and thats when I get an Internal Server error message....

Eventually page 2 & page 3 will be on different servers. Here is the code in the second page


<% dim lgreet,lvalue

lvalue = request.QueryString("TickNum")
if lvalue="" then lvalue="0"
lvalue=lvalue*1
Response.Cookies("RobertTicket")=lvalue
response.write "Cookie created and set"
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
{
function Next(){
self.location.href = "ViewCookie.asp"
}
}
</script>
</head>

<body>
<a href="Javascript:Next()">Next</a>
</body>
</html>

As I said this works fine but if I click next to go to viewcookie.asp (page 3) I get the error. Here is page3's code


<% If Request.Cookies("RobertTicket") = 1 Then
greet = "Tickets bought"
Else
greet = "No tickets"
End If
Response.Write greet
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

</body>
</html>

Any help is appreciated :beam:

prstudio
October 5th, 2004, 12:45 PM
doing it this way usually works...



<%
DIM TicketStatus
DIM greet
TicketStatus = Request.Cookies("RobertTicket")

If TicketStatus = 1 Then
greet = "Tickets bought"
Else
greet = "No tickets"
End If
Response.Write greet
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

</body>
</html>
let me know.

RabBell
October 5th, 2004, 12:54 PM
I changed the code a touch

If TicketStatus = "1" Then

and I don't get the error message anymore but TicketStatus is blank. Any ideas?

prstudio
October 5th, 2004, 03:33 PM
oops yeah the " " would help lol -
also is there an "end if" in here:


<% dim lgreet,lvalue

lvalue = request.QueryString("TickNum")
if lvalue="" then lvalue="0"

'insert end if here?

lvalue=lvalue*1
Response.Cookies("RobertTicket")=lvalue
response.write "Cookie created and set"

'screwing around here but looks like
'the value might be holding up in
'your if statement and returning null
'test it by putting this here:

DIM CookieWritten
CookieWritten = Request.Cookies("RobertTicket")
Response.Write CookieWritten
Response.Write lvalue
'the above should say the cookie was written
'tell what was in that cookie and give the
'value of lvalue
%>

wish i had more time but only had a few seconds to look -

also look at setting expire and other properties on them-

and it just looks like the script is working - but its not grabbing from the form

ok back to work for me

JakeMAN
October 5th, 2004, 06:34 PM
quick note: When you set a cookie using .asp, if you don't set the Expiration, then the cookie will be active on that browser window for however long it's open. If you open another browser window, the cookie will be gone, (unless the browser window is opened from the browser window that originally got the cookie).

just my .02¢
-JakeMAN

RabBell
October 5th, 2004, 06:58 PM
I added the piece of code suggested by prstudio on the second page and it confirmed that the correct value was being written and stored into the cookie but on the next page it's blank again

thanks JakeMan but it's all working from the same browser window. Just to be sure I edited the Expires date on the cookie but it didn't have an effect.

I tried editing the domain value as well but no luck

Any other ideas :beam:

prstudio
October 5th, 2004, 11:40 PM
try doing this:

if lvalue="" then lvalue="None"

delete this line below- it's not really doing anything anyways
lvalue=lvalue*1

and then change it to read you know if lvalue = "none"

feels like there is something missing here - cause i never have any problems like this with the cookies if everything is named right...just kind of throwing some troubleshooting out there...

RabBell
October 7th, 2004, 07:20 AM
ok I changed the code in the second page to something simple



<% dim lcook

Response.Cookies.Item("SimpleCookie") = "SimpleExample"

lcook = Request.Cookies.Item("SimpleCookie")
response.write " COOK:"& lcook
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
{
function Next(){
self.location.href = "ViewCookie.asp"
}
}
</script>
</head>

<body>
<a href="Javascript:Next()">Next</a>
</body>
</html>


and the value of the cookie is shown in the second page no problem, but here is the code for the third page


<% dim lcook

lcook = Request.Cookies.Item("SimpleCookie")
response.write " COOK:"& lcook
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

</body>
</html>

and guess what....the cookie is empty. All that is displayed on the screen is the word COOK.

someones gotta help me with this. I'm sitting with 2 large ASP books and I've read the sections on ASP cookies twice and I seem to be doing everything right. I am in the process of ripping the hair from my skull...someone help :(

Is this maybe an ASP version problem? This is all going on a clients server which I don't really have access to but is it possible they're running an old version of ASP that doesn't support cookies :h:

prstudio
October 8th, 2004, 02:20 AM
ok i just went ahead and wrote this real quick - this is a simple form to processing to view asp cookie script: use however you like:

FORM: - name "Cookiecutter.asp"

<html>
<head>
<title>Name</title>
</head>
<body>
<form method="POST" action="cookiemaker.asp">
<p>Name:</p>
<p><input type="text" name="name" size="20"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>


Processing - name - "cookiemaker.asp"


<%@Language=VBScript%>
<%
dim whatname
whatname = request.form("name")
if whatname = "prstudio" then
response.cookies("access") = "1"
else
response.cookies("access") = "0"
end if
response.redirect "cookiesheet.asp"
%>
view - name - "cookiesheet.asp"


<%@Language=VBScript%>
<%
DIM chocochip
chocochip = request.cookies("access")
response.write chocochip
if chocochip = "1" then
Response.Write "welcome!"
else
Response.Write "access denied"
end if
%>


it works.
you can also do all of this with one asp file...instead of 3
keep in mind also that i didnt set domain, expiration or path -

RabBell
October 8th, 2004, 04:44 AM
thanks pr, problem solved. Still not sure what the problem was but it works fine now (both your code and the pages I pasted above) :thumb:

prstudio
October 8th, 2004, 10:30 AM
i think it was a naming problem - glad its working