Search code examples
phpcookiessetcookie

Performing an action on cookie expiration


What I want to do it cause an action when a cookie expires. For example i have a cookie:

setcookie('loggedIn', true, time()+ 3600);

When the cookie expires I would like to be able to redirect them to a different web page automatically and call a php script that would log the user out.


Solution

  • You can check it via $_COOKIE.

    if(!isset($_COOKIE['loggedIn'])){
      header('Location: /path/to/another/page');
      exit;
    }
    

    You can code it in a separate file and include it in every page OR you can implement it in XHR.