Force Session not to exceed 30 minutes

Asked

Viewed 509 times

3

Galera I have a login system in PHP. I saved the login and the password of the user in a session.

This is how I start it:

// Inicia a sessão
session_start();

My question and how to make this session have a duration of 30 minutes, ie if the user leave the page stopped for 30 minutes the session has to be deleted.

  • save user password in a session ???

  • 1

    http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes

  • @Virgilionovic I already saved, but I want it erased after 30 minutes

  • Do not save password and session, the maximum the id of the user who is logged in or the identification you prefer, less important data!

  • You can save in the session the id and a date and time and as you request the page checking if the time has already exceeded 30 minutes and close the session.

2 answers

2


You can use the session_set_cookie_params to "set" a life time for the session

$time = strtotime('+30 minutes', 0);
session_set_cookie_params($time);
session_start();
  • 1

    There is a problem here, nothing guarantees that the cookie time will be respected, the user can extend as long as he wants. Need to do an IF on the server to ensure, more or less like the other answer.

2

See if that helps you:

session_start();

    if (isset($_SESSION['atividade']) && (time() - $_SESSION['atividade'] > 1800)) {

                session_unset();
                session_destroy();
            }
    $_SESSION['atividade'] = time();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.