IIS & Cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nefus
Forum Newbie
Posts: 16
Joined: Tue Mar 04, 2003 12:04 pm

IIS & Cookies

Post by nefus »

Hi guys, I seem to be able to set cookies with IIS, but not read them. I've been trying if (!$cookiename). Am I doing something wrong. Is there a way to pull the fields out of the cookie? Honestly, I am a rank beginner and would appreciate some pointers.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have a read of this:
viewtopic.php?t=511

You probably need to be using:

Code: Select all

if (!isset($_COOKIE['cookiename'])) {
Mac
nefus
Forum Newbie
Posts: 16
Joined: Tue Mar 04, 2003 12:04 pm

Post by nefus »

I use

setcookie("cookiename",$user,time()+(90*90*12),'sitename');


then to read it I have

if (!isset($_COOKIE["cookiename"]))
{ go to unauthorized page;
} else { do these things }

I have verified the cookie is being written. But I cannot get the second part of the code to work.

FYI - Unsure if this in related but I also cannot get session variables to be read either. I have checked and session.save_path is fine.


update: echo $_COOKIE['cookiename'] doesn't work. I get an error that says Undefined index: dmvalid in C:\Inetpub\clients\dm\htdocs\rulespost.php on line 1.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There's a bunch of things you need to be aware of when setting cookies - have you read this in the manual:
PHP manual wrote:Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.
Mac
nefus
Forum Newbie
Posts: 16
Joined: Tue Mar 04, 2003 12:04 pm

Post by nefus »

That command gives me a response. The others still do not work.

print_r($_COOKIE);
echo $_COOKIE['cookiename'];

will result in

Array ( [PHPSESSID] => c19b0e31595b2447f531da9af32889ac )
Notice: Undefined index: cookiename in C:\Inetpub\clients\dm\htdocs\rulespost.php on line 1
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Are you testing for the cookie on the same page on which you are setting it? That's the main reason I posted the quote from the manual above.

Mac
nefus
Forum Newbie
Posts: 16
Joined: Tue Mar 04, 2003 12:04 pm

Post by nefus »

No, I'm checking for the cookie on another page. :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Which browser are you using?

Mac
nefus
Forum Newbie
Posts: 16
Joined: Tue Mar 04, 2003 12:04 pm

Post by nefus »

IE6, IE5 and Mozilla 1.21. All 3 have been tested. This is very depressing. :-/.
Post Reply