0

I am trying to log a user out of my CMS after a set amount of time. By inactive I mean has not clicked the mouse or typed on there keyboard. So after 30 minutes of inactivity my log out function is ran.

There is already a log out function built in to the CMS I am using -

<?php
session_start();
if (isset($_SESSION['user_id'])){
    $login = 1;
}else{
    $login = 0;
}

function confirm_logged_in() {
    if (!isset($_SESSION['user_id'])) {
        //redirect
        header("Location: /_cms/login.php?login=0");
    }
}
function logout(){
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(), '', time()-4200, '/');
        }   
        session_destroy();
}

?>

Someone else wrote this code and it works. However I don't know the exact time it takes to log out an inactive user. The preset time is - 4200. What I want to find out is how long that takes to logout and if I can change it to any time I want. Can anyone advise?

2
Contributors
3
Replies
28
Views
2 Years
Discussion Span
Last Post by tobyITguy
0

The -4200 just sets the cookie expiration date to a date that has passed.

That way the cookie is deleted.

So changing the time in that particular function isn't gonna achieve what you want.

Edited by tobyITguy

0

Yes I have realised. I am trying something like this!

<?php
session_start();

function login(){
    //check login username/pass etc...
    $_SESSION['last_active_time'] = time();
}

function auth(){
   if($_SESSION['last_active_time'] < (time() - 180)){ //1800 is 30 minutes (time in seconds)
        logout(); //destroy the session in the logout function
    }
    else{
        $_SESSION['last_active_time'] = time();
    }
   //do some auth related things
}

function confirm_logged_in() {
    if (!isset($_SESSION['user_id'])) {
        //redirect
        header("Location: /_cms/login.php?login=0");
    }
}

function logout(){
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(), '', time()-1800, '/');
        }   
        session_destroy();
}
?>

Howver this is doing nothing. It logs in and out fine, but it does not log out an inactive user after 30 minutes(as it should).

0

You need to use javascript to determine mouse movement.
Your timer variable needs to be in javascript.

document.onmousemove = function(){your code here}

In your code you reset the timer every time the mouse moves.

Then you can make your javascript call your php function if the timer reaches 30mins without being reset.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.