Jump to content

logout script not working


redgunner2

Recommended Posts

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!!

Link to comment
Share on other sites

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 by benanamen
Link to comment
Share on other sites

$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. 

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.