Advanced Tutorial: Implementing Session Timeout in Your AppGini Application
Introduction
This tutorial guides advanced AppGini users through implementing a robust session inactivity timeout system in their own applications. You’ll learn how to combine server-side session management with client-side user experience enhancements, ensuring both security and usability for your users.
1. Why Session Timeout?
Session timeouts help protect your application by automatically logging out inactive users, reducing the risk of unauthorized access. This tutorial covers both the technical and user experience aspects of session timeout.
2. Server-Side: Enforcing Session Expiry
Note: For a more in-depth understanding of the code, refer to the Session Inactivity Timeout Implementation in AppGini Applications gist on GitHub. This gist provides a detailed breakdown of the session timeout implementation, including explanations of each part of the code and how it integrates with AppGini’s architecture.
a. Set the Session Lifetime
In your hooks/__bootstrap.php
, define how long a session should last (in seconds):
|
|
Related: Contents of the generated
hooks
folder >__bootstrap.php
b. Configure PHP Session Parameters
Still in __bootstrap.php
, ensure session garbage collection and cookie lifetime match your timeout:
|
|
c. Enforce Timeout on Every Request
Add the checkSessionActivity()
function to log out users who exceed the timeout. Also place this function in hooks/__bootstrap.php
:
|
|
Here is the full code to include in your hooks/__bootstrap.php
:
|
|
d. Call Session Check Globally
In your hooks/__global.php
, call this function at the top so it runs on every page:
|
|
Related: AppGini global hooks
3. Client-Side: User Experience & Warnings
a. Add Inactivity Timer Script
In your hooks/footer-extras.php
, add a script to:
- Monitor user activity (mouse, keyboard, touch)
- Show a warning before logout
- Log out the user after inactivity
- Send a keep-alive AJAX request if the user becomes active in the last minute
Related: Contents of the generated
hooks
folder >footer-extras.php
Example:
|
|
b. How It Works
- User activity resets the timer.
- Warning appears 60 seconds before logout.
- Keep-alive AJAX request is sent if activity occurs in the last minute.
- Logout happens if no activity.
The screenshot below shows the warning notification that appears in the bottom-right corner of the browser window, displaying a countdown timer indicating how many seconds remain before the user is logged out due to inactivity.
The video below demonstrates the network tab showing keep-alive AJAX requests sent to ajax_check_login.php
with the keep-alive
parameter. This happens when the user is active within the last minute before session timeout, ensuring the session remains valid.
4. Custom Pages: Automatic Coverage
Any custom page that includes the standard AppGini header and footer will inherit this session timeout system. For example:
|
|
No extra code is needed for custom pages!
5. Configuration & Customization
- Change timeout: Edit
SESSION_LIFETIME
in__bootstrap.php
. - Change warning time: Adjust the
60
in the warning logic infooter-extras.php
. - Change warning style: Edit the HTML/CSS in the warning div in
footer-extras.php
.
6. Troubleshooting & Tips
-
If sessions expire too quickly, check your PHP session settings and
SESSION_LIFETIME
. -
If the warning doesn’t appear, make sure
.username
exists in your page markup (that you haven’t removed it via other customizations). -
Use browser dev tools to monitor AJAX keep-alive requests.
-
The system works even if JavaScript is disabled, but users won’t see warnings.
-
Remember me won’t work with this system. If you want inactive users to be forced to log in again, it then makes sense that a user coming back after a few days or weeks will have to log in again, even if they had previously checked the “Remember me” option. You can remove the “Remember me” checkbox from the login form by adding the following code to your
hooks/footer-extras.php
inside the<script>
tag:1 2 3
$j(() => { $j('#rememberMe').closest('.checkbox').remove(); });
Note: For a more in-depth understanding of the code, refer to the Session Inactivity Timeout Implementation in AppGini Applications gist on GitHub. This gist provides a detailed breakdown of the session timeout implementation, including explanations of each part of the code and how it integrates with AppGini’s architecture.
Conclusion
With this approach, you get a secure, user-friendly session timeout system that works across your entire AppGini app—including custom pages. Advanced users can further customize the logic, UI, or integration as needed.