Setting a cookie with 2 parameters?

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
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Setting a cookie with 2 parameters?

Post by mikebr »

I am trying to set the following cookie:

setcookie("cookie_access",$_POST[password],"/");

but I get this error:

Warning: setcookie() expects parameter 3 to be long, string given in /Users/user/Sites/ckaccess.php on line 3

so I guess that means I need a third parameter so I added "time()" as follows:

setcookie("cookie_access",$_POST[password],time()+3500,"/");

to see if this was the problem and right enough it set the cookie, but then the example I am working on only uses "setcookie" with the two parameters so the cookie will expire when the user exits their browser and explains that to do this the parameter for time should be left blank, the "setcookie" line with two parameters is as in the book.

Anyone any ideas on why this will not work as described in the example?

Mike
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

because a cookie which expires when the user closes the browser is the same as a session so having an overlap is inefficient

im drunk again so excuse me
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I've used this on one of my sites.. it seems to work fine and will store the cookie for 1 week (even if the user exits the browser).

setcookie("CookieName","CookieValue",time()+604800);

Hope this helps!
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

I've used this on one of my sites.. it seems to work fine and will store the cookie for 1 week (even if the user exits the browser).

setcookie("CookieName","CookieValue",time()+604800);
I have no problem on setting the cookie when I use the 3 parameters but when I use 2 parameters only "so the cookie will expire when the user exits the browser" the cookie will not set........ as it happens I have moved onto the next stage in the book which is about using sessions so I would probably not ever need to use the example I mentioned as sessions seem to be the way to go on cookies, although it would have been good to know why the example will not work as per the book.

Thanks
Mike
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's not working because of the ', "/"' bit which PHP is expecting to be a time - you have to set the function parameters in a specific order:

Code: Select all

setcookie("cookie_access",$_POSTїpassword],"/");
should be

Code: Select all

setcookie('cookie_access', $_POSTї'password']);
On an unrelated note, although in the past a lot of people used to reference array elements without quoting them:

Code: Select all

$_POSTїpassword]
they really should be put into quotes

Code: Select all

$_POSTї'password']
because otherwise PHP first searches for a constant with the element's name and when it doesn't find it assumes that you meant to quote the element name and carries on. Basically this means that the scripting engine is doing extra work which it doesn't have to.

Mac
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

Yes, this did the trick and thanks for the tip about using quotes.

Thanks
Mike
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

If you don't want the browser to store the cookie (ie it should be destroyed when the browser is closed) pass a 0 for the third argument.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

or just leave it out - as he already dscovered
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

However, if he wanted to explicitly set the '/', or a subdirectory in the future you would have to specify the expiration time in order to do so. Knowing what value it defaults to (0) that makes the cookie ephemeral is useful in these situations.
Post Reply