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?

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.

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).

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.