简体   繁体   中英

Redirecting a user to login page after cookie expires

I currently have a login script that sets a cookie to expire after 8 hours. After the cookie has expired I want to redirect the user back to the login page? How would I achieve this?

If the login is on a different page , you could put this at the very top (before any output to the client):

if(!$_COOKIE['theNameOfTheCookie'])
    header('Location: loginPage.php');

But you will want to add some extra logic here to check for cookie values or whatever depending on how valuable the data is on the site. In the above example, someone could fake the cookie extremely easily.

or if you prefer sessions, just replace COOKIE with SESSION:

if(!$_SESSION['theNameOfTheCookie'])
    header('Location: loginPage.php');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM