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!
<?php
// set the expiration date to one hour ago
if(isset($_COOKIE['name'])) {
unset($_COOKIE['name']);
setcookie('name', '', time() - 3600); // empty value and old timestamp
}
?>
I test the cookie with this (on another page) and it still displays:
Sorry guys, I really appreciate it. I'm not sure what to do though. I'm still fairly intermediate.
The code above is most of my code.
I'm setting the cookie on a login page and displaying the cookie on an index page. Then Unsetting the cookie on a separate logout page. But when you go back to the index page it still shows the cookie value.
If anyone has a sample that would be great.
I'm killing the cookie with this code but it's not working
HeyThere wrote:I mean that what @social_experiment wanted to say is that you should check if your headers were not already sent
No.
What does your original cookie setting code look like, on the login page?
Have you tried to remove the unset($_COOKIE['name']) line? According to the PHP manual the correct way to unset a cookie is to use a time value that is in the past setcookie()
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
The "matching the cookie parameters" thing I mentioned before is making sure you use the same path and domain in the call to set the cookie as you do to remove the cookie. So