Log out routine

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Log out routine

Post by shasta59 » 2013-01-17 16:41

This is a repeat of the topic I posted in Tip and Tricks - it perhaps, would be better suited to this forum area.

Hello, in the old forum someone asked about a timer to end a session. Here is what I use. Note it will also affect the main login page and the user will have to refresh/reload the page. I have reasons for this and it works fine.

What I do is add two files in the root folder of the application (the code you put into these files is at the end of this post)
sessionend.php
sessiontrack.php


I did find this code online rather than writing my own but changed it to work nicely with appgini - for me it works great.

These files are loaded using an include in the header file.

In this code is a timer you can set for how long it stays alive. Then, after the timer expires, it clears all variables. One very very important fact. Do not set the timer too short. If a user is just filling in values in a form and the time expires then they have to log in again. This code is an idle timer. It only reset to zero when you actually submit something, go to another form/page etc. In other words you have to do something which makes a call to your database to get more data etc.

What I did is place the following code (in red) in the header.php file: It goes right below the first line in the header.php file. Make sure your changes look like what is below.

<?php $htmlUserBar=htmlUserBar();
include("$currDir/sessiontrack.php");
include("$currDir/sessionend.php");

?>

Here the code to put into the sessionend.php file you create:

<?php
if (isset($_SESSION["expired"])) {
print "Your session has expired. Please refresh the page and log in again"; //put whatever message you want here
$_SESSION["expired"] = '';
}
?>


Here is the code to put into the sessiontrack.php file

<?php
session_start();
if($_SESSION['session_count'] == 0) {
$_SESSION['session_count'] = 1;
$_SESSION['session_start_time']=time();
} else {
$_SESSION['session_count'] = $_SESSION['session_count'] + 1;
}

$session_timeout = 1800; // enter number of seconds here for session to live (in sec) - 60 = 1 minute - 1800/60 = 30 min

$session_duration = time() - $_SESSION['session_start_time'];
if ($session_duration > $session_timeout) {
session_unset();
session_destroy();
session_start();
session_regenerate_id(true);
$_SESSION["expired"] = "yes";
header("Location: http://yourwebpage/index.php"); // Redirect to Login Page - index.php - or whatever you wish
} else {
$_SESSION['session_start_time']=time();
}
?>


I have been using this now for approx 2 months now and in that time not one error has occurred other than users complaining they have to log in again.

It has not yet been tested with version 5. When that is done I will advise or change as needed.

Remember this works for me in my instance. Depending upon the changes you have made to the base code of appgini it may not work for you. However, since this is in the header file it loads nicely and should not give issues.

Enjoy

Alan Sopczak
Calgary, Alberta, Canada - Using Appgini 5.50 -

Post Reply