Page 1 of 1

Logout of inactive users not working???

Posted: Sun Sep 14, 2008 3:38 pm
by budimir
Hi guys,

I have a script for logging out inactive users. After a period of time that user is inactive, it's redirected to logoutd.php page where I set some statuses to 0 and destroy session. But, for some reason on logoutd.php I can't get values for username and password and that's why I can't set statuses to 0.

I can't see why this is so. Can you help me???

Here is a script which is loging out an inactive user:

Code: Select all

<?php
session_start();
$session_id = session_id();
 
include("db.php");
 
$upit = "SELECT * FROM korisnici WHERE session_id = '$session_id' && aktivan_s = 'ON'";
$rezultat = mysql_query($upit,$veza) or die (mysql_error());
$broj = mysql_num_rows($rezultat);
$row = mysql_fetch_array($rezultat);
        $user_id = $row["id"];
        $ime = $row["ime"];
        $korisnicko_ime = $row["korisnicko_ime"];
        $prezime = $row["prezime"];
        $userData["status"] = $row["status"];
        $lozinka = $row["lozinka"];
        $status = $row["status"];
        $aktivan = $row["aktivan"];
        $online_vrijeme = $row["online_vrijeme"];
        $g_servis = $row["g_servis"];
        $g_izvjestaji = $row["g_izvjestaji"];
        $g_popis_servisa = $row["g_popis_servisa"];
        $g_naljepnice = $row["g_naljepnice"];
        $g_poruke = $row["g_poruke"];
        $g_taskovi = $row["g_taskovi"];
        $g_zadaci = $row["g_zadaci"];
        $g_forum = $row["g_forum"];
        $g_korisnici = $row["g_korisnici"];
        $g_newsletter = $row["g_newsletter"];
        $g_putni_nalozi = $row["g_putni_nalog"];
        $vrsta_korisnika = $row["vrsta_korisnika"];
        $radno_mjesto = $row["radno_mjesto"];
 
$_SESSION['logged'] = TRUE;
$_SESSION['korisnicko_ime'] = $korisnicko_ime;
$_SESSION['lozinka'] = $lozinka;
 
 
function isLogged(){
    if($_SESSION['logged'] && $_SESSION['korisnicko_ime'] && $_SESSION['lozinka']){ # When logged in this variable is set to TRUE
        return TRUE;
    }else{
        return FALSE;
    }
}
 
# Log a user Out
function logOut(){
    $_SESSION = array();
    if (isset($_COOKIE[session_name()])) {
        setcookie(session_name(), '', time()-42000, '/');
    }
    session_destroy();
}
 
# Session Logout after in activity
function sessionX(){
    $logLength = 1800; # time in seconds :: 1800 = 30 minutes
    $ctime = strtotime("now"); # Create a time from a string
    # If no session time is created, create one
    if(!isset($_SESSION['sessionX'])){ 
        # create session time
        $_SESSION['sessionX'] = $ctime; 
    } else {
        # Check if they have exceded the time limit of inactivity
        if(((strtotime("now") - $_SESSION['sessionX']) > $logLength) && isLogged()){
            # If exceded the time, log the user out
            logOut();
            # Redirect to login page to log back in
            header("Location:http://localhost/erp/logoutd.php");
            exit;
        } else {
            # If they have not exceded the time limit of inactivity, keep them logged in
            $_SESSION['sessionX'] = $ctime;
        }
    }
} 
 
# Run Session logout check
sessionX(); 
?>
And this is script where I set the statuses to 0. But for some reason I'm not getting username and password values in WHERE clause and that's why it's not working. (Users are not logged out)

Code: Select all

<?php
include ("admin/servis/include/session.php");
 
$upit = "UPDATE korisnici SET session_id = '0', aktivan_s = 'OFF' WHERE korisnicko_ime = '".$_SESSION['korisnicko_ime']."' AND lozinka = '".$_SESSION['lozinka']."'";
$rezultat = mysql_query($upit,$veza) or die (mysql_error());
 
session_destroy();
header("Location:index.php");
exit;
mysql_close($veza);
?>
Please help. I'm going insane... :banghead:

Re: Logout of inactive users not working???

Posted: Sun Sep 14, 2008 5:46 pm
by JAB Creations
I'm hardly a pro but I know sessions expire X minutes after inactivity. If it's something that is adjustable via a $_SESSION option somehow then this would probably be a cinch.