Whos Online


People are wanting whos online scripts so here's a simple one.

This idea came from Diablosblizz.

Run this in phpmyAdmin
PHP Code
  1. ALTER TABLE `members` ADD `online` INT( 1 ) NOT NULL DEFAULT '0';


In login.php find..
PHP Code
  1. $_SESSION['id'] = "$user[id]";
  2. $_SESSION['password'] = "$user[password]";


And after put..
PHP Code
  1. $online = mysql_query("INSERT INTO `members` (`username`, `online`) VALUES('$username','1')");


This makes the user online as such. making the value 1 when they login and 0 when they logout.
So that makes the user online. but what if they wanted to logout?
In logout.php find..
PHP Code
  1. if(isset($_GET['logout'])){


And after put..
PHP Code
  1. $offline = mysql_query("INSERT INTO `members` (`username`, `online`) VALUES('$username','0')");


this shows them as offline
Want to view whos online. Nah didn't think so but here it is anyways.
PHP Code
  1. <?php
  2. include ("config.php");
  3. $sql = mysql_query("SELECT * from members where online == '1'");
  4. while ($online = mysql_fetch_array($sql))
  5. {
  6. $username = $online[username];
  7. echo ("- $username<br>");
  8. //displays the online users
  9. }
  10. ?>


this grabs all the users that have the value 1 which means they are online and displays it

be sure to look out for more tuts by be.
SkillMaster's Avatar
Views:
3,182
Rating:
Posted on Saturday 15th November 2008 at 09:57 PM
shedh
shedh's Avatar
this is right

<?php
session_start();
include"config.php";

$users = mysql_query("SELECT * FROM `members` WHERE `online`='1'");

while($online = mysql_fetch_array($users))
{
echo $online['username'] . " " . $online['ID'];
echo "<br />";
}
?>

A quick trip to W3SCHOOLS don't hurt anyone
Posted on Thursday 31st July 2008 at 03:46 PM
ShadowMage
ShadowMage's Avatar
SQL query is wrong.
Posted on Thursday 31st July 2008 at 12:51 PM
jambomb
jambomb's Avatar
I get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/*****/public_html/HighRize/main/whosonline.php on line 126
Posted on Tuesday 3rd June 2008 at 09:54 PM
DanielXP
DanielXP's Avatar
Why is this code doing a insert instead of an update to set if the user is online or not?
Posted on Tuesday 3rd June 2008 at 08:16 PM
Chikenty
Chikenty's Avatar
Alright well this is the 2nd tutorial where it has not worked simply because people just aren't testing it or are ripping it. I blow at coding and could see a problem from the start. Tutorial tells you to Insert into 'members' which simply only re-adds the name thats logged in with.
Im not sure how the bbcode on this comment system goes so i can only type the stuff out so here this goes in the login.php as stated in the tutorial:
$online = mysql_query("UPDATE `members` SET`online` = '1' WHERE `username` = '$username'");

This goes in the logout.php as shown above:
$offline = mysql_query("UPDATE `members` SET `online` = '0' WHERE `username` = '$logged[username]'");

And this is just a small add-on to where is shows whos online i linked it to the profile.
<?php

include("config.php");

$sql = mysql_query("SELECT * from members where online = '1'");

while ($online = mysql_fetch_array($sql))

{

$username = $online[username];

echo ("<a href='members.php?user=$username'>$username</a> ");

//displays the online users

}

?>
Posted on Tuesday 29th May 2007 at 01:29 AM
MCP
MCP's Avatar
Well see that is the problem. There are bits of code to edit into each login and logout. So the only thing is you need to know is I followed this tutorial to a T.

Still only a blank page.
Posted on Tuesday 29th May 2007 at 01:18 AM
James
James's Avatar
post you code so we can take a look
Posted on Monday 28th May 2007 at 10:34 PM
MCP
MCP's Avatar
Sorry bout the double post, but please help me in a PM.
Thanks
Posted on Monday 28th May 2007 at 03:13 PM
MCP
MCP's Avatar
All I see is a blank page when this is properly installed.

Help?
Posted on Monday 14th May 2007 at 11:11 PM
Diablosblizz
Diablosblizz's Avatar
Nah, my idea doesn't even work after what you posted in forums....