Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Timestamp - user timeout

Micky01Micky01 Posts: 34Member
Hi,

I have tried every way I can think of to achieve this but in the end I just cannot come to a conclusion.

I am trying to timeout a user(s) login if they have been inactive for a specified amount of time.
For example, if a user has been logged in for 15 minutes I want to log them out due to inactivity.
As it stands, users log in, and are assigned a session. I want to force the user to re-login if they have been inactive for 15 minutes.

I have tried endlessly but still can't get it to work.

Can anybody suggest a solution for this?

Thank you in advance.

Comments

  • tvientitvienti Posts: 230Member
    I can think of two ways.. server side and client side. Client side, use Javascript and set a 15 minute timer on each page... at the end of it, redirect to some script that logs them out. If they idle on a page, it'll trigger. If they closer their browser out, their session's dead anyhow so you're still accomplished your goal. And, of course, if they move to another page it resets the timer as you'd expect.

    But I have JS. I'd say it'd be more appropriately to just timestamp them each time they request a page. This'll be easy if you have some PHP code that's run on every page, if not you'll have to insert it at the top of every page.

    Anyhow, on each page hit run a snipper of code that does something like this:

    - Read the timestamp of their most recent page hit. If it's > 15 minutes, log them out and handle appropriately
    - Otherwise, overwrite it with the current time - this is their new timestamp

    Make sense?

    T

    : Hi,
    :
    : I have tried every way I can think of to achieve this but in the end I just cannot come to a conclusion.
    :
    : I am trying to timeout a user(s) login if they have been inactive for a specified amount of time.
    : For example, if a user has been logged in for 15 minutes I want to log them out due to inactivity.
    : As it stands, users log in, and are assigned a session. I want to force the user to re-login if they have been inactive for 15 minutes.
    :
    : I have tried endlessly but still can't get it to work.
    :
    : Can anybody suggest a solution for this?
    :
    : Thank you in advance.
    :

  • Micky01Micky01 Posts: 34Member
    Got it now, thanks.

    I have this working now.
    Thanks again for the help.
    Here's the code I used, may be useful to someone else.

    [code]
    in login.php
    -------------------
    $current_time = time(); // get the current time
    $_SESSION['loginTime']=$current_time; // login time
    $_SESSION['lastActivity']=$current_time; // last activity
    ......

    timeout.php (inc in each page)
    --------------
    $timeout_min = 15; // 15 minutes of inactivity
    $timeout_length = $timeout_min * 60;

    if ($current_time - $_SESSION['lastActivity'] > $timeout_length) {
    $_SESSION = array();
    if (isset($_COOKIE[session_name()])) {
    unset($_COOKIE[session_name()]);
    }
    session_destroy();
    echo "You have been logged out.......
    exit;
    }
    else
    $_SESSION['lastActivity'] = $current_time;

    ?>
    [/code]

    ---

    : I can think of two ways.. server side and client side. Client side, use Javascript and set a 15 minute timer on each page... at the end of it, redirect to some script that logs them out. If they idle on a page, it'll trigger. If they closer their browser out, their session's dead anyhow so you're still accomplished your goal. And, of course, if they move to another page it resets the timer as you'd expect.
    :
    : But I have JS. I'd say it'd be more appropriately to just timestamp them each time they request a page. This'll be easy if you have some PHP code that's run on every page, if not you'll have to insert it at the top of every page.
    :
    : Anyhow, on each page hit run a snipper of code that does something like this:
    :
    : - Read the timestamp of their most recent page hit. If it's > 15 minutes, log them out and handle appropriately
    : - Otherwise, overwrite it with the current time - this is their new timestamp
    :
    : Make sense?
    :
    : T
    :
    : : Hi,
    : :
    : : I have tried every way I can think of to achieve this but in the end I just cannot come to a conclusion.
    : :
    : : I am trying to timeout a user(s) login if they have been inactive for a specified amount of time.
    : : For example, if a user has been logged in for 15 minutes I want to log them out due to inactivity.
    : : As it stands, users log in, and are assigned a session. I want to force the user to re-login if they have been inactive for 15 minutes.
    : :
    : : I have tried endlessly but still can't get it to work.
    : :
    : : Can anybody suggest a solution for this?
    : :
    : : Thank you in advance.
    : :
    :
    :
Sign In or Register to comment.