php cookie doesn't set!!!

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
nariman
Forum Newbie
Posts: 4
Joined: Thu Jul 08, 2004 10:46 am
Contact:

php cookie doesn't set!!!

Post 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.
nariman
Forum Newbie
Posts: 4
Joined: Thu Jul 08, 2004 10:46 am
Contact:

Re: php cookie doesn't set!!!

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nariman
Forum Newbie
Posts: 4
Joined: Thu Jul 08, 2004 10:46 am
Contact:

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nariman
Forum Newbie
Posts: 4
Joined: Thu Jul 08, 2004 10:46 am
Contact:

Post by nariman »

I used setting the time to past and it is working properly, thanks.
Post Reply