I need help creating a who's online script in php for my site. I have found a few who's online script but I can't/don't know how to incoporate it into my site.
Just like the who's online script on vbulletin and phpbb, when a user logs in. I want it to display the username online.
This code checks to see if the users are logged in.
<?
$is_logged_in = "no";
if(isset($_COOKIE['username']) & isset($_COOKIE['password']) & isset($_COOKIE['u_id'])) {
$u_id = u_decrypt($_COOKIE['u_id']);
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'"));
if(stripslashes($_COOKIE['username']) == u_encrypt($user_info[username]) & stripslashes($_COOKIE['password']) == $user_info[password]) {
$is_logged_in = "yes";
} else {
$is_logged_in = "no";
}
} else {
$is_logged_in = "no";
}
if($is_logged_in == "yes") {
echo "You are logged in as $user_info[username].<br />";
} else {
include("form.php");
}
?>
I've read some of the post here about whos online but i can't seem to get it to work with my current login system.
CREATE TABLE visitors (
ID int(11) NOT NULL auto_increment,
Page text NOT NULL,
IP text NOT NULL,
Hits int(11) NOT NULL default '0',
LastClick int(11) NOT NULL default '0',
PRIMARY KEY (ID)
) TYPE=MyISAM;
===========
<?
error_reporting(0);
//Set Up Important Vars
$page = sprintf("%s%s%s","http://",$HTTP_HOST,$REQUEST_URI); //Finds the URL of the Current page
$endactive = time()+900; //The amount of time users have after their last click before they become offline: 900 = 15 mins
$enduser = time()+2419200; //The amount of time users have before their profile is deleted: 2419200 = 4 Weeks
// Join the Database :)
$GLOBALS['link']= mysql_connect("localhost", "root", "");
mysql_select_db("blog", $GLOBALS['link']) or die(mysql_error());
//Clear Unwanted Data
$DeleteOld="DELETE from visitors where LastClick >= '".$enduser."'";
mysql_query($DeleteOld, $GLOBALS['link']) or die(mysql_error()); $e++;
//Update the Users Data
$ThisUser = "SELECT * from visitors where IP = '".$REMOTE_ADDR."' limit 1";
$ThisUser = mysql_query($ThisUser, $GLOBALS['link']) or die(mysql_error()); $e++;
$UpdateInfo="INSERT into visitors values ('','".$page."', '".$REMOTE_ADDR."', '1','".(time()+900)."')";
while($loop = mysql_fetch_array($ThisUser)){
$UpdateInfo="UPDATE visitors set LastClick = '".(time()+900)."' where IP = '".$REMOTE_ADDR."'";
$UpdateHits="UPDATE visitors set Hits = Hits + 1 where (IP = '".$REMOTE_ADDR."')";
$UpdatePage="UPDATE visitors set Page = '".$page."' where (IP = '".$REMOTE_ADDR."')";
}
mysql_query($UpdateInfo, $GLOBALS['link']) or die(mysql_error()); $e++;
if($UpdateHits && $UpdatePage){
mysql_query($UpdateHits, $GLOBALS['link']) or die(mysql_error()); $e++;
mysql_query($UpdatePage, $GLOBALS['link']) or die(mysql_error()); $e++;
}
//Get the Results!
$GetVisitors = "SELECT * from visitors where LastClick >= '".time()."'";
$GetVisitors = mysql_query($GetVisitors, $GLOBALS['link']) or die(mysql_error()); $e++;
$g=0; $r=0; $c=0; $t=0; $p=0;
while($user = mysql_fetch_array($GetVisitors)){
if($user['Page']==$page){ $p++; }
if($user['Hits']>=0 && $user['Hits']<10){ $g++; }
if($user['Hits']>=10 && $user['Hits']<50){ $r++; }
if($user['Hits']>=50){ $c++; }
$t++;
}
Echo "There are $t People Online: <ul><li>$g guests online!<li>$r regulars online!<li>$c Cool People online!<li>$p People on this Page</ul>";
?>