Page 1 of 1
Setting a cookie with 2 parameters?
Posted: Mon Sep 30, 2002 5:40 pm
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
Posted: Mon Sep 30, 2002 5:51 pm
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
Posted: Mon Sep 30, 2002 5:52 pm
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!
Posted: Mon Sep 30, 2002 6:42 pm
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
Posted: Tue Oct 01, 2002 2:22 am
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:
they really should be put into quotes
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
Posted: Tue Oct 01, 2002 3:45 am
by mikebr
Yes, this did the trick and thanks for the tip about using quotes.
Thanks
Mike
Posted: Tue Oct 01, 2002 8:16 am
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.
Posted: Tue Oct 01, 2002 8:58 am
by mikeq
or just leave it out - as he already dscovered
Posted: Tue Oct 01, 2002 9:06 am
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.