/    Sign up×
Community /Pin to ProfileBookmark

Help to delete cookies needed

I am setting cookies on one page, let’s say, ‘register.php’. Then I am reading those cookies on another page (‘login.php’) and unsetting the cookies (deleting them) on this page. Everything works except for the last part: I am NOT able to delete the cookies. Would anyone help me?

Step1 (‘register.php’ page) — Setting the cookies:

[code=php]
$errors = array() // Suppose, this array is full with ‘errors’…

if(!empty($errors)) {
foreach($errors as $name => $value) {
setcookie(“errors[$name]”,$value,0,’/someDirectory/’);
}
[/code]

Step2 (‘login.php’ page) — Reading and deleting the cookies:

[code=php]
if(isset($_COOKIE[‘errors’])) {
foreach($_COOKIE[‘errors’] as $name => $value) {
print $name . ” ” . $value;
setcookie(‘errors[$name]’,”,1); // This line does NOT work.
}
}
[/code]

As I said above, I am able to set and read cookies but I am not able to delete them. Please, help!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ZnupiMar 18.2008 — From http://php.net/setcookie :

[i][b]expire[/b]: The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).[/i]

Also from http://php.net/setcookie :

[i][b]Example#2 setcookie() delete example[/b]

When deleting a cookie you should assure that the expiration date is in the past, to trigger the removal mechanism in your browser. Examples follow how to delete cookies sent in previous example:[/i]

[code=php]
<?php
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
?>
[/code]

The PHP manual is always your friend ?
Copy linkTweet thisAlerts:
@andkhlauthorMar 18.2008 — Thank you, but none of that works. Also, I did consult with the php.net site before I posted my message here.

The problem is NOT the expiration date of the cookie since [B]time() - 3600[/B] does NOT work either. The problem lies, I think, in the correct path to the cookies, however, I am not for sure.

My script only works if the SAME page sets, reads, and deletes the cookie, or in the case of two pages, both pages (the one that sets, and the other one that reads and deletes the cookies) are in the SAME directory. If these pages are in [B]different[/B] directories, the script does not work.

Still need help.
Copy linkTweet thisAlerts:
@ZnupiMar 18.2008 — Why not try this?
[code=php]
if(isset($_COOKIE['errors'])) {
foreach($_COOKIE['errors'] as $name => $value) {
print $name . " " . $value;
setcookie('errors[$name]','',time()-3600, '/someDirectory/'); // /someDirectory/ should be the same as in register.php
}
}
[/code]
Copy linkTweet thisAlerts:
@andkhlauthorMar 18.2008 — Thank you. It works now... That '/someDirectory/' part did the trick!
×

Success!

Help @andkhl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...