funbinod Posted October 4, 2014 Share Posted October 4, 2014 hi guys! this is somewhat related to my previous post about automatic session timeout. I calculated timeout time from sessions table and forced the user logout if (s)he exceeded the timeout limit. it did exactly what I expected from the following code $slaquery = mysqli_query($connect, "SELECT lastActive FROM session WHERE cid='$cid' AND uid='$uid'") or die ("Error: ".mysqli_error($connect));$slarow = mysqli_fetch_object($slaquery);$lastActive = $slarow->lastActive;$time = time();if ($time - $lastActive > 1200) { echo"<script>alert('Timeout!');</script>"; mysqli_query($connect, "DELETE FROM session WHERE cid='$cid' AND uid='$uid'") or die("Error: ".mysqli_error($connect)); $user->logout(); die(header("location: login.php"));} but it did not alert (or echo) the Timeout! as written on line 25. if I changed the code to create error session like this -- session_start();$_session['error'] = 'Timeout!';die(header('location: login.php'); using this, it prints other error sessions at login.php but doesn't print the 'Timeout!'. what could be the mistake..... Link to comment Share on other sites More sharing options...
niche Posted October 4, 2014 Share Posted October 4, 2014 Maybe it did. Depends on what logout() does. Link to comment Share on other sites More sharing options...
funbinod Posted October 5, 2014 Author Share Posted October 5, 2014 this is logout function public function logout() { $this->isLoggedIn = false; if (session_id() == '') { session_start(); } $_SESSION['isLoggedIn'] = false; foreach ($_SESSION as $key => $value) { $_SESSION['$key'] = ""; unset($_SESSION[$key]); } $_SESSION = array(); if (ini_get("session.use_cookies")) { $cookieParameters = session_get_cookie_params(); setcookie(session_name(), '', time() - 1200, $cookieParameters['path'],$cookieParameters['domain'],$cookieParameters['secure'],$cookieParameters['httponly']); } session_destroy(); } Link to comment Share on other sites More sharing options...
etsted Posted October 5, 2014 Share Posted October 5, 2014 maybe echo"<script>alert('Timeout!');</script>"; was interpreted as a string instead of javascript Link to comment Share on other sites More sharing options...
funbinod Posted October 5, 2014 Author Share Posted October 5, 2014 but if I use the same alert (inside php echo) elsewhere, it alerts perfectly.... or if it was interpreted as a string, then what u suggest me for that...??? Link to comment Share on other sites More sharing options...
Ingolme Posted October 5, 2014 Share Posted October 5, 2014 You probably got an error message that didn't show up because you have error reporting disabled. You can't print any content before sending a header. No echo statements or HTML. headers have to be the first thing sent to the client. When you redirect to another page, there's no point at all in displaying any content because the user is leaving the current page. Put the alert message on the destination page instead. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now