Forum Moderators: open

Message Too Old, No Replies

classic asp cookies issue

         

adrianbromfield

3:05 pm on Feb 25, 2008 (gmt 0)

10+ Year Member



Hi,

does anybody know why or a way around this problem i am getting.

if i do this..

if 2 = request.cookies("car_id") then

this will return true

but if i swap to for a variable like

if var = request.cookies("car_id") then

it wont return true. And i have made sure that the var is equal to 2 for testing. I cant understand why this is. or how i can do what im wanting to do with this happening. it needs to be a variable as var will change as its an id for a specific car.

anyhelp would really be appreciated.

thanks,
adrian

vfoo

10:54 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



Be sure to ensure that your variables are correctly typed, often that is the only problem.

ASP uses "Variant" types for almost everything, so even though your cookie "car_id" is set to the numeral 2, it will very often output as the string "2".

So when you say does "2" = 2, the answer is also not always true.

(One of the many reasons I hate classic ASP is that there are many things that do not always do the same thing in different server environments)

Note: There are other ways to ensure your variable type is correct, that said, I like the cStr method because of it's simplicity. The reason for the format of cStr("" & Value) is to ensure that the cStr function has an output when Value is Null.

So an example:

(Assuming Classic ASP here):

Dim MyCookie 'As String
Dim MyTest 'As String

'Setup the vars and ensure types are correct
MyCookie = cStr("" & Request.Cookies("cookie_name"))
MyTest = "2"

'Perform the Test
If MyCookie = MyTest Then
Response.Write "Test Passed"
Else
Response.wRite "Test Failed"
End If

HTH
vfoo

adrianbromfield

2:00 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



Hey Vfoo,

you are a star! I couldnt understand as when testing I tried as "2" and as 2, and they both worked fine but as soon as i used variable it didnt. So maybe how it was storing in the variable was different.

I have tried out your suggestion and it works perfectly so thanks a bundle. I guess it is always best to make sure it is of same type.

Thanks again.