Setting a cookie with 2 parameters?
Moderator: General Moderators
Setting a cookie with 2 parameters?
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
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
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.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);
Thanks
Mike
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
should be
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
Code: Select all
setcookie("cookie_access",$_POSTїpassword],"/");Code: Select all
setcookie('cookie_access', $_POSTї'password']);Code: Select all
$_POSTїpassword]Code: Select all
$_POSTї'password']Mac