redgunner2 Posted October 25, 2015 Share Posted October 25, 2015 Hello, I would appreciative of any help so I can fix my logout script - I have been spent hours trying work out different things and I need help of possible!! logout.php $mysqli = new mysqli('localhost', 'db', 'db', 'db'); # check connection if ($mysqli->connect_errno) { echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; exit(); } // update status to offline $sql = "SELECT status from users WHERE id={$_SESSION['username']}"; $result = $mysqli->query($sql); $user = $result->fetch_array(); $timestamp = $user['status'] - 300; $sql = "UPDATE users SET status={$timestamp} WHERE id={$_SESSION['username']}"; $result = $mysqli->query($sql); ## finally destroying the session // unset all session variables $_SESSION = array(); // destroy the session cookie if(isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-50000, '/'); } // destroy the session session_destroy(); redirect_to("login.php"); ?> I get the following error - Fatal error: Call to a member function fetch_array() on a non-object in /home/www/site/logout.php on line 17 I think what is happening is $user = $result->fetch_array(); returning no information but I am able to display the value of my username in any page which has the session, many thanks for your help in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/298806-logout-script-not-working/ Share on other sites More sharing options...
benanamen Posted October 25, 2015 Share Posted October 25, 2015 (edited) Why are you even selecting the users status from the database? You already know they are online, just update the status to offline. It appears you are doing something with the status that will require some explanation. It simply should just be status = 1 or status =0 Is the status column an actual timestamp column? And you should be using an actual number for a user id, not the username. Edited October 25, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298806-logout-script-not-working/#findComment-1524241 Share on other sites More sharing options...
0x00 Posted October 25, 2015 Share Posted October 25, 2015 debugging wise I'd echo $_SESSION['username'] to check see whats in there... then, on getting the result I'd check if there was a result Quote Link to comment https://forums.phpfreaks.com/topic/298806-logout-script-not-working/#findComment-1524257 Share on other sites More sharing options...
hansford Posted October 25, 2015 Share Posted October 25, 2015 $sql = "SELECT status from users WHERE id={$_SESSION['username']}"; if ( ! $result = $mysqli->query($sql)) { //do something } If you want to log users out after so much time of inactivity then put a timestamp column in your table and each time they do an activity check the current time against the db time and log them out if it's been too long or update the timestamp column. Quote Link to comment https://forums.phpfreaks.com/topic/298806-logout-script-not-working/#findComment-1524260 Share on other sites More sharing options...
redgunner2 Posted October 25, 2015 Author Share Posted October 25, 2015 Thanks to all - i got this working now Quote Link to comment https://forums.phpfreaks.com/topic/298806-logout-script-not-working/#findComment-1524282 Share on other sites More sharing options...
benanamen Posted October 25, 2015 Share Posted October 25, 2015 (edited) Standard forum protocol is to post what you did to fix your problem so other people reading this thread or having the same problem may benefit from it. Not to mention several people have taken their time to try to help you for free. Edited October 25, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298806-logout-script-not-working/#findComment-1524287 Share on other sites More sharing options...
redgunner2 Posted October 25, 2015 Author Share Posted October 25, 2015 I got this working on my own and would be too complex to explain when I only posted a snippet... Again thanks for your positive comment. Quote Link to comment https://forums.phpfreaks.com/topic/298806-logout-script-not-working/#findComment-1524290 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.