Hello,

I have a bit of a problem, I am think its not the script that has the problem, but wanted to be sure before contacting the server masters.

In my website, I can make cookies, and sessions, I can echo them successfully.

But when I logout, it doesn't work,

Lets take the cookie example:

I set the cookie like:

setcookie('id',$username,time()+3600);

this is the logout, It used to work on other servers ok.

setcookie('id','',time()-3600);
require_once("header.php");
require_once("logout.php");
require_once("footer.php");

This is how I check if the cookie is set:

if(!isset($_COOKIE['id']))
{
echo "Sign In";
}
else
{
echo "Sign Out";
}

when I check if the cookie is there, it has this strange attitude.
After I login and make the cookie, no errors appear when I surf through the site's pages. (the 'sign out' button appears)

when I logout, I can surf the site with no problems (the 'Sign in' button appears), but at the index page still its writing ('Logout') giving the fact the I am using the same header file for all the pages.

One more detail (take a look at the logout code): when I logout I still get the 'Sign out' button appearing. for the first time giving the fact that I clear the cookie before echoing the header. but If I refresh the logout page again right after logging out the first time I get the 'Sign in' button again.

what is happening? and what is the problem with the index, is it the php.ini file? should I contact the server masters?

Thanks

Dani AI

Generated

The behavior you describe (logout shows the old state on the first load, then the correct state after a refresh) is a very common source of confusion. Two things are almost always involved: (1) cookies set or removed with PHP are sent in response headers and only affect the next HTTP request unless you also update the runtime state, and (2) a cached copy of the index page or a cookie path/domain mismatch can make one page behave differently from the rest. 's symptoms fit both patterns.

Quick checklist to diagnose

  • Use the browser dev tools (Network tab) or curl to inspect the response headers from your logout request and confirm the Set-Cookie header is present and has the same path/domain as the cookie you originally set. Example:
    curl -s -D - -o /dev/null "https://example.com/logout.php"
  • Verify the index page is really being generated by PHP each request (not served from a static cache or CDN). Append a query string like ?nocache=1 to rule out caching.
  • Look for mismatched domain (example.com vs www.example.com), path, or secure/HTTP-only flags when deleting the cookie.

Practical fixes to apply immediately

  • After sending the cookie-expiration header, also clear the runtime variables so header checks reflect the new state during the same request:
    session_start();
    $_SESSION = array();
    session_destroy();
    unset($_COOKIE['id']);
  • If proxy/server caching is the problem, send no-cache headers on pages that depend on login state:
    header('Cache-Control: no-cache, no-store, must-revalidate');
    header('Pragma: no-cache');
    header('Expires: 0');

Notes and references

  • When you delete a cookie, you must match the same path and domain parameters you used when creating it; otherwise the browser won’t remove it. See the PHP cookie notes for details: setcookie — PHP manual and general cookie behavior: MDN Cookies.

Please can anyone give me an idea?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.