[PHP] Logout problems

Status
Not open for further replies.

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Basically once a user has logged in im using
PHP:
$db->query("UPDATE users SET online = '1' WHERE username = '".$db->escape($row['username'])."'");
to update the users online status (this works)

However when i come to log out i have:
PHP:
include('includes/core.php');

unset($_SESSION['user_id']);
unset($_SESSION['user_login_session']);
unset($_SESSION['user_ip']);

$db->query("UPDATE users SET online = '0' WHERE username = '".$db->escape($row['username'])."'");

header('Location: '.$site.'');

When i come to log out, it does not change my online status to '0' in the db field

Any ideas why this is happening?
I have looked around on google and 90% of most common answers are using the same as what i have
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Change it to
PHP:
include('includes/core.php');
die($row['username']);
unset($_SESSION['user_id']);
unset($_SESSION['user_login_session']);
unset($_SESSION['user_ip']);

$db->query("UPDATE users SET online = '0' WHERE username = '".$db->escape($row['username'])."'");

header('Location: '.$site.'');

What does it output?
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Originally it logged me out but didnt change the online field to 0

Using what you just give me, returns a white (blank) page with 127.0.0.1/logout as the url and hasnt logged me out
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Then $row['username'] is null, thus making the SQL query fail. Since it doesn't know the where to change 'online' from 1 to 0.

That's the problem.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Right i have now added this in
PHP:
include('includes/core.php');

unset($_SESSION['user_id']);
unset($_SESSION['user_login_session']);
unset($_SESSION['user_ip']);

$get_user = $db->query("SELECT username FROM users WHERE username = '.'username'.'");
if($db->num_rows($get_user)) {
$row = $db->fetch_array($get_user);
$db->query("UPDATE users SET online = '0' WHERE username = '".$db->escape($row['username'])."'");
}

header('Location: '.$site.'');

Yet its still not working, have i gone wrong somewhere?
 
Status
Not open for further replies.

Users who are viewing this thread

Top