Asked  7 Months ago    Answers:  5   Viewed   24 times

When I want to remove a Cookie I try


I see in my cookie browser from firefox that the cookie still exists. How can I really remove the cookie?



You May Try this

if (isset($_COOKIE['remember_user'])) {
    setcookie('remember_user', null, -1, '/'); 
    return true;
} else {
    return false;
Wednesday, March 31, 2021
answered 7 Months ago

You're supposed to give a UNIX timestamp of when the cookie will expired (calculated since the epoch) as the third argument to the function call.

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 suggest you read the documentation for setcookie.

Saturday, May 29, 2021
answered 5 Months ago

Make sure you have a domain that is known by both server and client. echo $_SERVER['HTTP_HOST'] should tell you the exact same domain that your browser has. If not, cookie will not be accepted by the browser.

Make sure your server and client time is perfectly correct. Browser will reject a cookie with a wrong datetime.

Do not write any other code and just do:

$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day 
// expiration
echo date("H:i:s d.m.Y")."<br>";
echo $_SERVER['HTTP_HOST']."<br>";

and refresh the page twice.

Also check out manual at:

Saturday, May 29, 2021
answered 5 Months ago

The MaxAge of -1 signals that you want the cookie to persist for the duration of the session. You want to set MaxAge to 0 instead.

From the API documentation:

A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
Tuesday, June 8, 2021
answered 5 Months ago

I got the answer.
Actually I was deleting the node from my xml file, but I was not writing it to my xml file.
I was missing following part

TransformerFactory transformerFactory = TransformerFactory.newInstance();  
Transformer transformer = transformerFactory.newTransformer();   
DOMSource source = new DOMSource(doc);   
StreamResult result = new StreamResult(selectedFile); 
transformer.transform(source, result); 

After long time study of code I found this silly mistake, so I corrected it.
Anyways thanks for your reply.

Thursday, September 2, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :