Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:19:41+00:00 2026-05-16T04:19:41+00:00

I am new to php and am trying to build a website. I would

  • 0

I am new to php and am trying to build a website. I would like to logout user session if he is idle for some time. I searched the web but i couldn’t find the proper code. How do you check whether user is idle or not in php?. Can anyone help me please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:19:41+00:00Added an answer on May 16, 2026 at 4:19 am

    There are a few ways to do this. Here are a couple…

    • set a session expiry time, such that after a certain amount of time, the session expires and is no longer valid.

    • set a ‘time’ flag as session data, and check if their session is still ‘new enough’ to keep them logged in each pageload.

    I would opt for the second choice, as it can be difficult to set the right values in PHP such that the session expires securely, and the way you want it to. With the second option, all you have to do is make sure the session will not expire before you want it to, which is easier.

    Code example for option 2:

    //on pageload
    session_start();
    
    $idletime=60;//after 60 seconds the user gets logged out
    
    if (time()-$_SESSION['timestamp']>$idletime){
        session_destroy();
        session_unset();
    }else{
        $_SESSION['timestamp']=time();
    }
    
    //on session creation
    $_SESSION['timestamp']=time();
    

    EDIT:

    Your comment explains that you’d actually like to keep track of mouse events and other things on the client side to determine if the user is idle. This is more complicated. I will give a general solution, and then offer a couple suggestions for optimizations and improvements.

    To accomplish what you’ve described, you must track clientside activity (mouse movements, keyboard strokes etc) and process that information on the server side.

    Tracking clientside activity will require javascript event handlers. You should create an event handler for every action you want to count as ‘not being idle’, and keep track (in a javascript variable) of when the last time they’ve been idle is.

    Processing that info on the server side will require you to use ajax to send the last time they’ve been idle to the server. So every few seconds, you should send the server an update (using javascript) which specifies how long the user has been idle.

    A couple extra suggestions:

    1. You should not rely on this ajax solution as the only way to track user activity, as some users will not have JS enabled. So, you should also track user activity on pageload. Accordingly you should probably not set the idle time too low (at least for non-JS users), as non-JS users will not be sending any data to the server until pageloads occur.

    2. You should send updates to the server via ajax as infrequently as possible about user activity to decrease demand on the server. To do this, simply have javascript keep track of when the user would time out. If it gets to the point where the user is about to time out (say, in about a minute or so), then and only then ping the server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to PHP and web development. I am trying to build a
I am new to PHP and was trying to build RestFull service in PHP
I am trying to build a website with mysql and php. This is the
I am new to wordpress. I am trying to build a custon website based
I am new to PHP and am trying to build a facebook application. In
I'm trying to build a website using object-oriented functionality, and running into some issues.
I am trying to rewrite some server php code which logs into a website,
I am new to PHP and trying to get the following code to work:
I am new to php and trying to write a login function. Bit stuck
I'm new to PHP and trying to get my head around security. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.