Jump to content

[SOLVED] auto logout if inactive!


budimir

Recommended Posts

Hey guys, I need an idea how to do auto logout if user is inactive for more then 30 mins.

 

I tried deifferent ways, but it's not working.

 

Right now I'm using sessions.

 

Is it a better idea to use cookies????

 

This is my login form now:

 

<?php
session_start();

include ("admin/servis/include/db.php");
$vrijeme = date("Y-m-d H:i:s");
// Define $myusername and $mypassword 
$myusername = mysql_real_escape_string($_POST['myusername'],$veza); 
$mypassword = mysql_real_escape_string($_POST['mypassword'],$veza); 

$sql="SELECT * FROM korisnici WHERE korisnicko_ime='$myusername' and lozinka='$mypassword' and aktivan = '1'";
$result=mysql_query($sql,$veza) or die (mysql_error());

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
$upd = "UPDATE korisnici SET session_id='".session_id()."', aktivan_s = 'ON', online_vrijeme = '$vrijeme' WHERE korisnicko_ime='$myusername' and lozinka='$mypassword'";
mysql_query($upd,$veza) or die (mysql_error());


header("location:index_glavni.php");
exit;
}
else {
header("Location:logoutn.php");
exit;
}
?>

Link to comment
Share on other sites

Yep, I have put it in a file called session.php which I include on top of eache page.

 

Here is the code of session.php

 

<?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());
$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"];
	$vrsta_korisnika = $row["vrsta_korisnika"];


function isLogged(){
    if($_SESSION['logged']){ # 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 = 120; # 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: ../../index.php");
            exit;
        }else{
            # If they have not exceded the time limit of inactivity, keep them logged in
            $_SESSION['sessionX'] = $ctime;
        }
    }
} 

if (!$user_id){
header("Location:logoutd.php");
exit;
}
# Run Session logout check
sessionX(); 
?>

Link to comment
Share on other sites

OK, I was able to make the script to wrok.

 

But the problem is with redirecting the page!!! It's not redirecting, but show error message!!!

 

This is the error message from Firefox 3:

"REDIRECT LOOP.  Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

 

I don't see what is causing this BUG.

 

Here is the complete code:

<?php
session_start();
$session_id = session_id();

$_SESSION['logged'] = TRUE;

function isLogged(){
    if($_SESSION['logged']){ # 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 = 120; # 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: ../../logoutd.php");
            exit;
        }else{
            # If they have not exceded the time limit of inactivity, keep them logged in
            $_SESSION['sessionX'] = $ctime;
        }
    }
} 

include("db.php");

$upit = "SELECT * FROM korisnici WHERE session_id = '$session_id' && aktivan_s = 'ON'";
$rezultat = mysql_query($upit,$veza) or die (mysql_error());
$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"];
	$vrsta_korisnika = $row["vrsta_korisnika"];

if (!$user_id){
header("Location:logoutd.php");
exit;
}		

# Run Session logout check
sessionX(); 
?>

Link to comment
Share on other sites

OK,

 

So, finally the auto logout is working, but still a small problem.

 

When a user is inactiv for 30 mins, it's redirected to logoutd.php page where I set all the status to 0 and then it's redirected to index.php for login.

 

The problem is, the status are not set to 0.

 

Here is the logoutd.php code:

<?php
include ("admin/servis/include/session.php");

$korisnicko_ime = $_GET["korisnicko_ime"];
$lozinka = $_GET["lozinka"];

$upit = "UPDATE korisnici SET session_id = '0', aktivan_s = 'OFF' WHERE korisnicko_ime='$korisnicko_ime' and lozinka='$lozinka'";
$rezultat = mysql_query($upit,$veza) or die (mysql_error());

session_destroy();
header("Location:index.php");
exit;
?>

Link to comment
Share on other sites

OK,

 

To sum up.

 

The problem is:

 

When a user is inactive for more then 30 mins he get's redirected to a page called logoutd.php where I'm setting some status. But the status is not set for some reason. I don't know why.

 

Here is the session.php which is redirecting page to logoutd.php

<?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"];
	$vrsta_korisnika = $row["vrsta_korisnika"];

$_SESSION['logged'] = TRUE;

function isLogged(){
    if($_SESSION['logged']){ # 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 = 120; # 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: ../../logoutd.php");
            exit;
        } else {
            # If they have not exceded the time limit of inactivity, keep them logged in
            $_SESSION['sessionX'] = $ctime;
        }
    }
} 



# Ovo uzrokuje grešku sa beskonacnom petljom!!! (Provjeriti)	
//if ($broj == 1)
//{
//	header("Location:index_glavni.php");
//	exit;
//} else {
//	header("Location:logoutd.php");
//	exit;
//}		

# Run Session logout check
sessionX(); 
?>

 

Here is the logoutd.php where I'm setting status:

<?php
include ("admin/servis/include/session.php");

$upit = "UPDATE korisnici SET session_id = '0', aktivan_s = 'OFF' WHERE korisnicko_ime='$korisnicko_ime' and lozinka='$lozinka'";
$rezultat = mysql_query($upit,$veza) or die (mysql_error());

session_destroy();
header("Location:index.php");
exit;
?>

 

This is my last problem for loging out users.

 

Help!!!  :o

Link to comment
Share on other sites

Well, actually I have set the session to last 3 mins, so i could test it. And it's working fine. When it's inactive for more then 3 mins, it goes to logoutd.php page.

 

But the problem is that on logoutd.php page I'm setting some values to 0, and that query is not working. It's not getting username and password for some reason and I can't see why.

 

Username and password I'm getting from session.php and on logoutd.php I'm including that file on top, but for reason it just want show any values???

 

I have posted both files.

 

Session.php - Here I have user info and logout function.

logoutd.php - Here I have a query for setting the values to 0. (for some reason it's not getting values from session.php)

 

Help???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.