Session time of the user

Asked

Viewed 3,956 times

2

Good community. When the user logs in I add to the database the day and time this user logged in.

The file init.php, which contains the login and which I use on all my pages I have put the following code:

if(($_SESSION['last_login'])>2){
//echo "ok";
}else{

header("location: logout.php?timeout=1");
}  

Now, I wanted the user after 30 minutes to be redirected to the logout page. Am I doing it correctly? Date format 2016-01-29 14:19:45

2 answers

2


I don’t see the need to record in banco de dados, use session variables only. Here’s a simple example doing this in 30 seconds:

login.php

<?php

session_start();
if(!isset($_SESSION['start_login'])) { // se não tiver pego tempo que logou
    $_SESSION['start_login'] = time(); //pega tempo que logou
    // adiciona 30 segundos ao tempo e grava em outra variável de sessão
    $_SESSION['logout_time'] = $_SESSION['start_login'] + 30; 
}

// se o tempo atual for maior que o tempo de logout
if(time() >= $_SESSION['logout_time']) { 
    header("location:logout.php"); //vai para logout
    session_destroy();
} else {
    $red = $_SESSION['logout_time'] - time(); // tempo que falta
    echo "Início de sessão: ".$_SESSION['start_login']."<br>";
    echo "Redirecionando em ".$red." segundos.<br>";
}

?>

logout.php

<?php

echo "logout\n<br>";
session_start();
session_destroy();

?>
<a href="login.php">Voltar</a>

To put 30 minutos place 30 * 60.

  • In my case, I put your piece of code in my init.php, which is the page that is always present in all the others, yes? My login file only runs when the user logs in.

  • @Ivcs I put in my init.php but it is not sending the user to logout.php. o echo of the code gave this: Início de sessão: 1454174519 Redirecionando em 30 segundos.

  • For this I should see the file structure. If init is on all pages you can put the code there. Check if the logout.php is in the same init folder, if do not change the path of header. And make sure you reload or change a page after the 30-second time has passed, as php does not check this in real time, only when there is a refresh.

  • 1

    @Ivcs perfect friend :) put in init.php page Thanks :)

0

PHP already has a parameter session.gc_maxlifetime, that can be changed.

Edit the PHP.ini:

session.gc_maxlifetime = 1800

This will cause the session to expire after 1800 seconds (30 minutes).

Check if there is a session:

So just check if there is a session:

<?php

if(isset($_SESSION['sua_sessao'])){
// Se houver sessão - OK
}else{
header('location: logout.php');
// Se não houver - Redireciona para logout.php
}

?>

The session will expire in 30 minutes and redirect to logout.php.

Problems and corrections:

Suppose a user has NEVER logged in, if he accesses the page will be redirected to logout.php, because he also has no session, which in my opinion would be wrong.

So create a cookie when you log in and check as follows:

<?php

//...
}else{

if(isset($_COOKIE['login'])){
// Se existe o cookie, que já indica que o usuário já se conectou alguma vez
    header('location: logout.php');
    // Redireciona para logout.
}else{
// Se o visitante caiu de paraquedas na página e nunca fez o login
    header('location: login.php');
    // Redireciona para o login.
}

?>

Browser other questions tagged

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