Clear session after 15 minutes of user inactivity using php


If your website/ online application have user login system, session check is an integral part of the coding logic. If user is logged in an admin panel, or any other restricted area only for privileged user you must perform user inactivity check which will help reducing security risk if user forgets to logout. In this tutorial I will explain how to clear session after 15 minutes of user inactivity using PHP.

View Demo

Scenario check before going through the tutorial

Before rushing into things let’s first get a scenario check on what we are going to do.

  • User logs into admin panel and a new session is created for him.
  • User can logs out via clicking logout link.
  • If user does not interact with the application for 15 minutes, the application logs him out automatically.

Logic behind checking user inactivity and clearing session

Moving on, we have to first make a login system where user can logs in via user name and password. You can learn about creating secure login system with PHP and Mysql here. In your login check page you have to set two session variables.

Now include this code in every page that is only accessed by privileged user except the logout page. Including this code in logout page will stop this script to work in our desired way.

You logout page must be like this. It will destroy all data and redirect the user back to index page.

View Demo


About Author

Entrepreneur & Dreamer

I am a passionate Software Professional, love to learn and share my knowledge with others.

Software is the hardware of my life.


Leave A Reply

CommentLuv badge

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.