简体   繁体   中英

PHP logout of 10 mins inactivity fails

code part of Login.php

            else if( ($row['Password'] == md5($pass)) ){
                    $_SESSION['user'] = $row['FName'];
                    $_SESSION['last_activity'] = time();
                    header("Location: welcome.php");
            }

Whole code of welcome.php

<?php
session_start();

if( !isset($_SESSION['user']) ){ //session  verification
    header("Location: login.php");
}
else{
    echo "Welcome, ". $_SESSION['user']. "<br>";
    echo "<a href='logout.php'> Logout </a>";
}

if( $_SESSION['last_activity'] < (time() - 600) ){ //time in seconds, 10 minutes
    session_destroy();
    header("Location: login.php");
    exit;
}
else{
    $_SESSION['last_activity'] = time();
}

?>

Question: Even I refresh the page after 10 minutes, welcome.php page kept logged in. What is wrong? Thanks

Try this;

$last_activity = time() - $_SESSION['last_activity'];

if( $last_activity >= 600 ){ //time in seconds, 10 minutes
    session_destroy();
    header("Location: login.php");
    exit();
}

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