I got the idea to create a simple “who’s online” script that is really small and very customizable. I may post more customized versions in the future. Here is the code:
Plain Text Version
<?php class WhosOnline{ var $mysqlhost='localhost'; var $mysqluser='root'; var $mysqlpass=''; var $mysqldatabase='epco'; var $mysqltable='whosonline'; /** * $idletime = amount of time before an idle user is * considered inactive. In Seconds * * @var string */ var $idletime='600'; /** * $deletetime = amount of time before a user is * deleted from the database for being idle * * @var string */ var $deletetime='1200'; /** * $bail - If set to true, it will auto return all the * functions if there is an error, so no mysql/class * errors will be displayed * * @var bool */ var $bail=true; /** * $die - If set to true, the current PHP script will * exit completely. * * @var bool */ var $die=false; /** * $quit - Once the class bails, it will return if true * * @var bool */ var $quit=false; /** * $mysqlerrors - If true, will display mysql_errors * instead of standard errors * * @var bool */ var $mysqlerrors=false; /** * $images - Array containing numbers 0-10 and corresponding * images * * @var array */ var $images = array(); /** * $username - String that relates to the session variable for the user name. * * @var string */ var $username = 'Guest'; /** * WhosOnline::WhosOnline($host,$user,$password,$database [,$table] [,$autoupdate]) * * For reverse compatibility with older versions of PHP * * @param string $host MySQL Host (localhost) * @param string $user MySQL database user * @param string $password MySQL user password * @param string $database MySQL database * @param optional string $table MySQL table * @param optional bool $autoupdate * @return null Always null */ function WhosOnline($host,$user,$password,$database,$table='',$autoupdate=false) { $this->_construct($host,$user,$password,$database); return; } /** * WhosOnline::_construct($host, $user, $password, $database [,$table] [,$autoupdate]) * * This creates the database connection and is created * by simply typing: * * $whosonline = new WhosOnline('localhost','root','','epco'); * * By utilizing $table and $autocomplete, you do not need to WhosOnline::Update() * * @param string $host MySQL Host (localhost) * @param string $user MySQL database user * @param string $password MySQL user password * @param string $database MySQL database * @param optional string $table MySQL table * @param optional bool $autoupdate * @return null Always null */ function _construct($host,$user,$password,$database,$table='',$autoupdate=false) { $this->mysqlhost=$host; $this->mysqluser=$user; $this->mysqlpass=$password; $this->mysqldatabase=$database; $this->Connect(); if($table!='' && $autoupdate==true) { $this->TableSet($table); $this->Update(); } return; } /** * WhosOnline::Connect() * * Connects to a mysql database and stores the connection. MySQL * variables must be set prior to connection, either through * WhosOnline::_construct() or by setting individual variables * * @return null Always null */ function Connect() { $this->dbh = @mysql_connect($this->mysqlhost,$this->mysqluser,$this->mysqlpass,true); if (!$this->dbh) $this->Error('Could not connect to the database. Please check your connection settings.'); if($this->quit) return; if(!@mysql_select_db($this->mysqldatabase,$this->dbh)) $this->Error('Invalid database. Possibly typo or user does not have privileges on that database'); return; } /** * WhosOnline::TableSet($table, $create=true) * * Checks for the table. If it doesn't exist it will create the * table if $create is true, otherwise it will return an error. * * @param string $table MySQL table where data is stored * @param bool $create If true, a new table will be created, else echo MySQL error * @return null Always null */ function TableSet($table, $create=true) { if($this->quit) return; $this->mysqltable=$table; $tables = array(); $query = @mysql_query("SHOW TABLES FROM ".$this->mysqldatabase,$this->dbh); while ($row = mysql_fetch_array($query)) { $tables[] = $row[0]; } if(!in_array($table,$tables) && $create==true) $this->CreateTable(); else if(!in_array($table,$tables)) $this->Error('Table not found in database.'); return; } /** * WhosOnline::CreateTable() * * Creates a MySQL table to store the data. * * @return null Always null */ function CreateTable() { $query="CREATE TABLE ".$this->mysqltable." (sid varchar(100),time int(100), user varchar(100))"; if(!mysql_query($query,$this->dbh)) $this->Error('Unable to create table.'); return; } /** * WhosOnline::Error($message,$mysql) * * Displays an error message. If $bail is set to true, it will close the class. * If $die is set to true, it will terminate the PHP display * * @param string $message Error message to be echoed * @param string $message Error from mysql * @return null Always null */ function Error($message,$mysql = '') { if($this->die && $this->mysqlerrors) die($mysql); else if($this->die) die($message); if($this->mysqlerrors) echo $mysql; else echo $message; if($this->bail) $this->quit=true; return; } /** * WhosOnline::DisplayMysqlErrors() * * When called, it will turn on mysql error display * * @return null Always null */ function DisplayMysqlErrors() { $this->mysqlerrors=true; return; } /** * WhosOnline::_destruct() * * Class destructor * * @return bool Always true */ function _destruct() { return true; } /** * WhosOnline::Update() * * Update the users online * * @return null Always null * */ function Update() { if($this->quit) return; $sid = session_id(); $query="SELECT * FROM ".$this->mysqltable." WHERE sid='$sid'"; $time=time(); $rows=mysql_num_rows(mysql_query($query,$this->dbh)); if($rows!=0) $query2="UPDATE ".$this->mysqltable." SET time='$time', user='".$this->username."' WHERE sid='$sid'"; else $query2="INSERT INTO ".$this->mysqltable." (sid,time,user) VALUES ('$sid','$time','".$this->username."')"; if(!@mysql_query($query2,$this->dbh)) $this->Error('Error updating the user database.'); $this->Delete(); return; } /** * WhosOnline::Data($return = 'INT') * * Returns the number of users considered "Online". * If $return = 'ARRAY', an array of user names will be returned. * * @return int $users The number of people online * @return array $online An array with the individual people online */ function Data($return = 'INT') { if($this->quit) return; $now=time(); $load=(int)$now-$this->idletime; $query = "SELECT * FROM ".$this->mysqltable; $result=mysql_query($query,$this->dbh); $users=0; $members=0; $guests=0; $online= array(); while($row=mysql_fetch_array($result)) { if((int)$row['time']>$load) { if($row['user']!='Guest') { $online[]=$row['user']; $members++; }else $guests++; $users++; } } $online['count']=$users; $online['members']=$members; $online['guests']=$guests; if($return=="INT") return $users; else return $online; } /** * WhosOnline::DisplayImages * * Like WhosOnline::Data(), displays the number of users online * but formatted a user defined pictures. * * @return string $output A html string with the digits represented by images */ function DisplayImages() { if(count($this->images)!=10) $this->Error('Only '.count($this->images).' images defined.'); if($this->quit) return; $now=time(); $load=(int)$now-$this->idletime; $query = "SELECT * FROM ".$this->mysqltable; $result=mysql_query($query,$this->dbh); $users=0; while($row=mysql_fetch_array($result)) { if((int)$row['time']>$load) $users++; } $array=str_split($users); $output=''; foreach($array as $digit) { $output.="<img src='{$this->images[$digit]}'>"; } return $output; } /** * WhosOnline::Delete() * * Deletes users who have "idled" to long from the database * * @return null Always null */ function Delete() { $now=time(); $load=(int)$now-$this->deletetime; $query = "SELECT * FROM ".$this->mysqltable; $result=mysql_query($query,$this->dbh); $count=0; while($row=mysql_fetch_array($result)) { if((int)$row['time']<$load) $sid[$count]="SID='{$row['sid']}'"; $count++; } if(isset($sid)) { if(count($sid)>0) { $todelete=implode(" OR ",$sid); mysql_query("DELETE FROM ".$this->mysqltable." WHERE $todelete"); } } return; } /** * WhosOnline::SetImage($int,$src) * * Sets images to be displayed by counter if WhosOnline::ImageDisplay=true; * * @param int $int The number * @param string $src Location of image * @return null Always null */ function SetImage($int,$src) { if(is_int($int) && $int<10 && $int>=0) { $this->images[$int]=$src; } return; } /** * WhosOnline::UpdateUser($sessionvar, $type) * * Updates WhosOnline::username to match the session variable that is provided. * $type indicates if the variable is in the session array or just another string. * * @param string $sessionvar Location of username in $_SESSION array * @param string $type Either "SESSION" or "STRING" * @return null Always null */ function UpdateUser($sessionvar, $type = "SESSION") { if(!empty($sessionvar) && array_key_exists($sessionvar,$_SESSION)) $this->username=$_SESSION[$sessionvar]; return; } } ?>
What other features are you guys looking for for this? It is the most popular page on the website, and I can easily add more features. Just need to know what you are looking for.
want to be able to show logged usernames and not just a counter
i have php site with backend database to verify users and want to be able to visually put the logged users name online when they log in
can you do this
Very possible. I will work on this tomorrow. I have a few ideas on how to do this and will update the post.
very interested in having site show which member is online…picture then word that says online/offline or a green dot or red dot representing online or offline
I’m with chrisjones. I’d like to have the user name with a red/green icon indicating online/offline status on my site too.
You could set that up pretty easy:
That should do it I think?
Maybe I’m stupid, any ways I’m fairly new at this php. I’ve now read and searched on your sit for the code to inplant in my header.tpl file where I want the whosonline box shown, can’t seem to find it. Maybe I’ve overlooked…
Check out the tutorial and same codes:
Tutorial: http://notan00b.com/tutorials/whosonline-tutorial/
Sample: http://notan00b.com/code-bin/whos-online-counter/whos-online-counter-sample-page/
They go over how to use it. It is going to require a little bit of configuration depending on what you want it to do. These are just jumping off points.
WHERE IS THE MySQL TABLES EXAMPLE OR FILE???????????????????? HOW TO SETUP MySQL???? Thanks
Man, take the effort and watch the code. It generates the mysql table for itself.
Sorry, i have a question.
Is the code above the ultimate and entire code version with the implementation of the handling of usernames? Or it still must be updated?
Thank you, and sorry for my bad english.
Thanks for the script !!
$wo = new WhoOnline($dbhost,$dbuser,$dbpass,$dbname);
$wo->username = $user->firstName // retrieved from my access class
$wo->TableSet("whosonline");
$wo->Update();
worked perfectly.. even shows my registered users name in the table and guest if not reged..
Only thing I would change is its a little confusing to require the db params in the constructor and also having manual db params in the class