PHP Class: Online Guest Count System Using PHP
Here is what we’ve got here? A PHP Online Guest Class
What Does This Do?
This class saves any guest that visits your site saving some vital details about them, refreshing the time set and counting number of guests online
How to Configure This:
Firstly, set up your database connection by editing these lines of code:
private $db = array ( 'host' => 'localhost', 'dbuser' => 'root', 'dbpwd' => '', 'dbname' => 'test' );
By changing the above to your database details, the database connection has been configured. Now creating table please run this SQL Query:
CREATE TABLE IF NOT EXISTS online_users ( id INT NOT NULL AUTO_INCREMENT, session_id TEXT NOT NULL, user_ip VARCHAR ( 200 ), ua TEXT, url_location TEXT, online_time TEXT NOT NULL, http_referer TEXT, PRIMARY KEY ( id ) )Engine=MyISAM
Created successfully? Now you are good to go! 🙂
How to Use This Class
- Firstly, include the class using
<?php require_once ( 'OnlineGuests.php' );
- Then initiate the class, the class do have a default argument of 30 seconds of refreshing the guests’ online status. So you can change this by adding new argument, like am using 15seconds to refresh below
$online_guests = new OnlineGuests ( 15 );
- Cool! Class initiated, of course you wish to be saving guests. Lets callup the function to save any guest that hits your site
$online_guests->saveGuests ();
Congratulations! The class has been configured. Want more cool features?
- Get arrays of online guests, use the getOnlineGuests () function which the default argument is PHP Array, you can also request for JSON
echo $online_guests->getOnlineGuests ( 'json' );
- Get total sum of online guests
echo $online_guests->online;
Download code here and enjoy! 🙂
PS: Works with session_start() function, else won’t work!