budimir Posted September 5, 2008 Share Posted September 5, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 5, 2008 Share Posted September 5, 2008 Here is a pre-built function free to use: http://phpsnips.com/snippet.php?id=39 Quote Link to comment Share on other sites More sharing options...
budimir Posted September 5, 2008 Author Share Posted September 5, 2008 Thanks, a lot!! I'll try it out! Quote Link to comment Share on other sites More sharing options...
budimir Posted September 5, 2008 Author Share Posted September 5, 2008 Hey The Little Guy, Can you give me a little help with that script. It's not working for me... I don't know why??? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 5, 2008 Share Posted September 5, 2008 Hey The Little Guy, Can you give me a little help with that script. It's not working for me... I don't know why??? Can you be a little more descriptive? Where did you put the code? Is it being called on every page? Quote Link to comment Share on other sites More sharing options...
budimir Posted September 5, 2008 Author Share Posted September 5, 2008 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(); ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 5, 2008 Share Posted September 5, 2008 try moving those functions up, right under session_start(); Quote Link to comment Share on other sites More sharing options...
budimir Posted September 5, 2008 Author Share Posted September 5, 2008 I tried moving it to the top, but still not working... I don't understand why... Any ideas? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 5, 2008 Share Posted September 5, 2008 oh yeah... do you have a $_SESSION['logged'] ?? if not, you will need to create one when you set your session variables when a user logs in. it should be set to TRUE: $_SESSION['logged'] = TRUE; Quote Link to comment Share on other sites More sharing options...
budimir Posted September 7, 2008 Author Share Posted September 7, 2008 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(); ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 7, 2008 Share Posted September 7, 2008 this is probably the problem. if not $user_id, then redirect to logoutd.php, if not $user_id, then redirect to logoutd.php, etc. ad infinitum if (!$user_id){ header("Location:logoutd.php"); exit; } Quote Link to comment Share on other sites More sharing options...
budimir Posted September 7, 2008 Author Share Posted September 7, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
budimir Posted September 7, 2008 Author Share Posted September 7, 2008 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!!! Quote Link to comment Share on other sites More sharing options...
budimir Posted September 9, 2008 Author Share Posted September 9, 2008 Anyone????? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 9, 2008 Share Posted September 9, 2008 how long is your default session? my thought is that the session may expire before 30 minutes, leaving you without any session variables for that user. Quote Link to comment Share on other sites More sharing options...
budimir Posted September 10, 2008 Author Share Posted September 10, 2008 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??? Quote Link to comment Share on other sites More sharing options...
budimir Posted September 14, 2008 Author Share Posted September 14, 2008 I don't think that is the problem. I have tried setting logout after 3 mins for testing and still I can't get the values passed to the query on logoutd.php I don't understand why isn't passed to it! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.