Creating an auto exit page in php

I am trying to write an automatic logout script that seems to work, but does not meet my expectations, I don’t know what I am doing wrong, I want to put timeout.php on each page so that when the user does not work, he automatically logs out and forwards it to the page login , but when I put timeout.php to my page add users, where an administrator adds a user, overriding the link to add to your user page and putting login page which also goes nicely (ie in the form of goes from its position) it looks like this

this is timeout.php code

  <?php $_SESSION = 0; if($_SESSION['session_count'] == 0) { $_SESSION['session_count'] = 1; $_SESSION['session_start_time']=time(); } else { $_SESSION['session_count'] = $_SESSION['session_count'] + 1; } $session_timeout = 10; // (in sec) $session_duration = time() - $_SESSION['session_start_time']; if ($session_duration > $session_timeout) { session_unset(); session_destroy(); session_start(); session_regenerate_id(true); $_SESSION["expired"] = "yes"; header("Location: login.php"); // Redirect to Login Page } else { $_SESSION['session_start_time']=time(); } ?> 

I want him to automatically log out and redirect himself to the expired login page, and I want him to put it on every page without breaking the form or blocking the page.

+4
source share
1 answer

This may have nothing to do with the problem: why are you using $_SESSION["expired"] = "yes"; in your if tag? I see no reason to set this session variable if you are not using it. (Perhaps you are using it somewhere else, I do not know).

Are you also sure that your forwarding is correct? Try echo header("Location: login.php"); This should help you verify the correctness of your redirects.

And if it’s not: check that you have correctly entered your timeout.php link on the add users page. (For example: imagine that you use if-else on the add users page, and it should be in your if-tag, but you used it in your else tag)

0
source

Source: https://habr.com/ru/post/1478922/


All Articles