How to implement a PHP session timeout on your own

How to implement a PHP session timeout on your own
by Janeth Kent Date: 05-12-2013 php webdev

To Implement a session timeout on your own,  you can use a simple time stamp that denotes the time of the last activity and update it with every request.

You can also use an additional time stamp to regenerate the session ID periodically to avoid attacks on sessions.

Enjoy the code!


  1. if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
  2. // last request was more than 30 minutes ago
  3. session_unset(); // unset $_SESSION variable for the run-time
  4. session_destroy(); // destroy session data in storage
  5. }
  6. $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
  7.  
  8. /*
  9. You can also use an additional time stamp to regenerate the session ID periodically to avoid attacks on sessions like session fixation:
  10. */
  11. if (!isset($_SESSION['CREATED'])) {
  12. $_SESSION['CREATED'] = time();
  13. } else if (time() - $_SESSION['CREATED'] > 1800) {
  14. // session started more than 30 minutes ago
  15. session_regenerate_id(true); // change session ID for the current session an invalidate old session ID
  16. $_SESSION['CREATED'] = time(); // update creation time
  17. }
  18.  
  19. //note that session.gc_maxlifetime should be at least equal to the life time of this custom expiration handler (1800 in this example).

 

 

 

original source & credits

 
by Janeth Kent Date: 05-12-2013 php webdev hits : 14606  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.

 
 
 

Related Posts

Examine the 10 key PHP functions I use frequently

PHP never ceases to surprise me with its built-in capabilities. These are a few of the functions I find most fascinating.   1. Levenshtein This function uses the Levenshtein algorithm to calculate the…

How to Track Flight Status in real-time using the Flight Tracker API

The Flight Tracker API provides developers with the ability to access real-time flight status, which is extremely useful for integrating historical tracking or live queries of air traffic into your…

What is a JWT token and how does it work?

JWT tokens are a standard used to create application access tokens, enabling user authentication in web applications. Specifically, it follows the RFC 7519 standard. What is a JWT token A JWT token…

PHP - The Singleton Pattern

The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a method for limiting the number of instances of an object to just one.…

How to Send Email from an HTML Contact Form

In today’s article we will write about how to make a working form that upon hitting that submit button will be functional and send the email (to you as a…

The State of PHP 8: new features and changes

PHP 8.0 has been released last November 26: let's discover together the main innovations that the new version introduces in this language. PHP is one of the most popular programming languages…

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

The most popular Array Sorting Algorithms In PHP

There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations,…

MySQL 8.0 is now fully supported in PHP 7.4

MySQL and PHP is a love story that started long time ago. However the love story with MySQL 8.0 was a bit slower to start… but don’t worry it rules…

A roadmap to becoming a web developer in 2019

There are plenty of tutorials online, which won't cost you a cent. If you are sufficiently self-driven and interested, you have no difficulty training yourself. The point to learn coding…

10 PHP code snippets to work with dates

Here we have some set of Useful PHP Snippets, which are useful for PHP Developers. In this tutorial we'll show you the 10 PHP date snippets you can use on…

8 Free PHP Books to Read in Summer 2018

In this article, we've listed 8 free PHP books that can help you to learn new approaches to solving problems and keep your skill up to date.   Practical PHP Testing This book…