Page 1 of 1

php cookie doesn't set!!!

Posted: Thu Jul 08, 2004 10:46 am
by nariman
I am trying to learn cookie in php and the first step does not work. I have tried these:

$value = 'test';
setcookie ("TestCookie", $value);

$value = 'test';
setcookie ("TestCookie", $value,0,'/');

$value = 'test';
setcookie ("TestCookie", $value,0);

and none of them sets a cookie when I test with isset(). any reason?
My browser is set to accept cookies.

Re: php cookie doesn't set!!!

Posted: Thu Jul 08, 2004 10:55 am
by nariman
nariman wrote:I am trying to learn cookie in php and the first step does not work. I have tried these:

$value = 'test';
setcookie ("TestCookie", $value);

$value = 'test';
setcookie ("TestCookie", $value,0,'/');

$value = 'test';
setcookie ("TestCookie", $value,0);

and none of them sets a cookie when I test with isset(). any reason?
My browser is set to accept cookies.
I checked my cookies and it shows that a phpsessionID is set but why isset() does confirm it? when I move it to the top of HTML it works!!! how can I put the code inside the HTML?

Posted: Thu Jul 08, 2004 12:23 pm
by pickle
The middle one is the one you want (probably :) ). That will make the cookie not expire, and it will be valid over your whole domain. Try doing just the middle one then seeing what print_r($_COOKIE); gives you.

Posted: Thu Jul 08, 2004 12:42 pm
by nariman
Thank you.

I figured it out, I copied and pasted all the cookie section to the top of the page above headers and the problem is fixed.

How can I delete a cookie. I am setting the time to time()-40000 and it doesn't release it!

Posted: Thu Jul 08, 2004 12:51 pm
by pickle
The simple answer is: You can't. I've never heard of a way to actually DELETE a cookie. The closest you can do is expire it. That way it will become invalid. Or, you could set it's value to '' and instead of doing a simple isset() check, actually check the value. I think the latter is the better method.

Posted: Thu Jul 08, 2004 2:52 pm
by nariman
I used setting the time to past and it is working properly, thanks.