Jump to content

15min-Expiration Logout Script


Swarfega

Recommended Posts

Hey.

 

 

I've made a script that would auto-logout users have they not been active for 15min, but it seems to not be working properly.. I've included these two files in ALL my php's that display data, so that each and every single user contributes to the Currently-Online-Members script whenever they click one of the links.

 

 

update_timer.php

<?php
session_start();
require_once('config.php');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}

$getdate = date('H:i:s');

$SQL = "UPDATE potatis SET last_activity='".$getdate."' WHERE uid='".$_SESSION['SESS_MEMBER_ID']."'";
mysql_query($SQL) or die(mysql_error());
?>

 

logout_timer.php

<?php
require_once('config.php');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}

$getdate = date("H:i:s");
$date = explode(":", $getdate);

$date[0] = $date[0] * 60 * 60;
$date[1] = $date[1] * 60;
$totalsecondsnow = $date[0] + $date[1] + $date[2];


$SQL = "SELECT uid, last_activity FROM potatis";
$RESULT = mysql_query($SQL);

while($row = mysql_fetch_assoc($RESULT)) {
$thendate = explode(":", $row['last_activity']);
$thendate[0] = $thendate[0] * 60 * 60;
$thendate[1] = $thendate[1] * 60;
$totalsecondsthen = $thendate[0] + $thendate[1] + $thendate[2];

$totalsecondsallowed = $totalsecondsthen + 900;

if($totalsecondsnow > $totalsecondsthen) {
$qry = "UPDATE potatis SET connected='offline' WHERE uid='". $row['uid'] . "'";
mysql_query($qry) or die(mysql_error());
} else {
$qry = "UPDATE potatis SET connected='online' WHERE uid='". $row['uid'] . "'";
mysql_query($qry) or die(mysql_error());
}
}
?>

 

 

But it does not seem to work properly.. One person signed on at 13:53 my time, the time when I checked the database, her last_activity was 13:53:43 or something, and the current time was 13:55:23, yet it still showed her being offline, is that not just very wrong?

 

 

Where did I go wrong in the script to make that happen?

 

 

Edit: It does however not put MY user as Offline when I click on a link, my last_activity was 14:01

Link to comment
https://forums.phpfreaks.com/topic/272692-15min-expiration-logout-script/
Share on other sites

if($totalsecondsnow > $totalsecondsthen) {

 

Shouldn't that be...

if($totalsecondsnow > $totalsecondsallowed) {

 

The way you have it now is like saying, if the current time is greater than their last activity, log them off. Although I'm not even sure the way above will still work

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.