budimir Posted September 14, 2008 Share Posted September 14, 2008 Hey guys, I have a script which is loging out inactive users and redircting them to the logout page. The problem is, when an inactive user is redirected to the logout page the sesion variables username and password are not passed to the logout page and because of that the user stays logged in. I don't see why is this happening. Can someone take a look and tell me what am I missing? Here is the script for loging out inactive users: <?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']){ # 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 logout page, where I set all the status to 0 and logout users. Here I dont get values for username and password, so the query is not executing. I don't know why!!! <?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; ?> Help!!! Quote Link to comment https://forums.phpfreaks.com/topic/124204-solved-values-not-passed-because-of-a-function/ Share on other sites More sharing options...
Goldeneye Posted September 14, 2008 Share Posted September 14, 2008 It looks like you forgot session_start(); at the top of your login page. Quote Link to comment https://forums.phpfreaks.com/topic/124204-solved-values-not-passed-because-of-a-function/#findComment-641281 Share on other sites More sharing options...
budimir Posted September 14, 2008 Author Share Posted September 14, 2008 No, that's not it. I have a session_start() on top of the login page, also I have session_start() on session.php where I'm keeping all the data information important for the session. And on the logout page I include session.php . Everything else connected to session is working. When I hit manually logout button everything is OK. But when I script detects that a user is inactive and redirects to logout page that it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/124204-solved-values-not-passed-because-of-a-function/#findComment-641283 Share on other sites More sharing options...
Goldeneye Posted September 14, 2008 Share Posted September 14, 2008 What might be happening is that the user's session has already timed out by the time the script has detected that user an inactive. If that's the case, then the username and password $_SESSION variables have already been destroyed causing the logout script to work with null/undefined variables. Quote Link to comment https://forums.phpfreaks.com/topic/124204-solved-values-not-passed-because-of-a-function/#findComment-641290 Share on other sites More sharing options...
budimir Posted September 14, 2008 Author Share Posted September 14, 2008 I have ruled out that option too. I have set session timeout to 3mins and checked what's happening. But, it's the same thing. When I try to echo username and password variables in logout.php I can't get anything out. So, I think that these variables are not passed further because of the function which is logoing out inactive users, but I don't know why? Is there any other way to pass these variables to logout page through the function? Quote Link to comment https://forums.phpfreaks.com/topic/124204-solved-values-not-passed-because-of-a-function/#findComment-641297 Share on other sites More sharing options...
budimir Posted September 15, 2008 Author Share Posted September 15, 2008 Anyone??? Ideas??? Quote Link to comment https://forums.phpfreaks.com/topic/124204-solved-values-not-passed-because-of-a-function/#findComment-641912 Share on other sites More sharing options...
budimir Posted September 15, 2008 Author Share Posted September 15, 2008 Finally guys! I solved my problem in a totally simplier way. if ($_SESSION['last_reload'] != "" && $_SESSION['last_reload'] < strtotime("-180 minutes")) { header("Location:http://localhost/erp/logoutd.php"); } else { $_SESSION['last_reload'] = time(); } If anyone needs it, it's working perfectly and it's totally simple. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/124204-solved-values-not-passed-because-of-a-function/#findComment-642181 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.