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.
php cookie doesn't set!!!
Moderator: General Moderators
Re: php cookie doesn't set!!!
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?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.
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.