Search code examples
phpcachingcookieshttp-redirectcache-control

setcookie and redirect loads page from cache


I have a page that sets a cookie and then redirects the user to the same page:

setcookie('name', $value, time() + $time, '/', '.domain.com');
header("Refresh: 0; url={$to}");

The problem is that after refresh the page gets loaded from cache and I can't use the cookie. I have to manually refresh (cmd+r) the page to actually be able to use the cookie.

I've also tried using

header("Location: {$to}");

for the refresh but with no success.

One method that works is appending a timestamp to the end of the page, like

$to .= '?' . time();

butthis is not something I want the user to see in his address bar.

I couldn't find any way to force the browser to reload the page rather than load it from its cache.

Thanks!


Solution

  • try to do this headers

    header("Cache-Control: no-cache");
    header("Pragma: no-cache");