Heya, im a little board at the moment so might as well share some knowledge lol.
This is how you create a Users Online Class in PHP5:
Step 1.
Firstly we need to create a database to hold the information within, the code below would be executed within PhpMyAdmin or what ever MySql Software you are using
Step 2.
Ok so lets begin by create the class structure
What this has done is basically created an object in witch you can use throught your site. (an object holds several methods and the methods are whats used to do different task.)
Step 3.
Assigning some variables to hold some temporary data
Step 4.
in this step were going to create a function thats know as a constructor, what this means is that when you first initiate the class this function will run to to prepare any data before you actually use the class
Step 5.
As you can see from the above code we have created a construct method, now we tell the class what to do when its first called
Step 6.
Ok the class should construct now and what we need to do is to create some methods/functions so we will begin with the function that gets the IP address.
Ok so when we run the getIp() function this will assign the var $ip with the IP address contained in $_SERVER
Step 7.
here we will create another function that will get the location of the user.
Step 8.
Ok now we have created all the methods/functions we need for the __constructor we can now update the constructor function
Step 9.
Ok no when we instantiate the object it will automatically track the time,ip.location before we run any methods/functions
But below were going to create 2 primary functions that will furfill the main perpose of the script, they are:
Step 9.1
Here were going to update the table with a method
Step 9.2
Ok so by now we have update the table and we want to be able to get the LATEST data from mysql witch will tell us how many UNIQUE users we have online
Ok so this function is basicaly calculating how many UNIQUE ip hits have been inserted into the data within the above timeframe.
Step 10
Ok so now the class is finally complete we can safe that into a php file now and name it usersonline.class.php and upload it to your server!
Step 11
Ok so now you want to get the count into your files, we will use the index file for example..
Open your index.php file or whatever file your adding this too and add the following block of code
Ok now everything should be finished and i hope you enjoyed the tutorial.
P.S. Stay Warez
This is how you create a Users Online Class in PHP5:
Step 1.
Firstly we need to create a database to hold the information within, the code below would be executed within PhpMyAdmin or what ever MySql Software you are using
Code:
CREATE TABLE IF NOT EXISTS `usersonline` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`ip` varchar(15) NOT NULL,
`location` varchar(512) NOT NULL DEFAULT '/',
'timestamp` varchar(15) NOT NULL DEFAULT '',
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2231 ;
Step 2.
Ok so lets begin by create the class structure
PHP:
<?php
class usersonline{
}
?>
What this has done is basically created an object in witch you can use throught your site. (an object holds several methods and the methods are whats used to do different task.)
Step 3.
Assigning some variables to hold some temporary data
PHP:
<?php
class usersonline{
var $timeout = 7200; //this is the time you want to track your users back to
var $ip = false; // The ip address of the user
var $location = false; //The location within your site you want to track
var $time = false; //This will just hold the current time
}
?>
Step 4.
in this step were going to create a function thats know as a constructor, what this means is that when you first initiate the class this function will run to to prepare any data before you actually use the class
PHP:
<?php
class usersonline{
var $timeout = 7200; //this is the time you want to track your users back to
var $ip = false; // The ip address of the user
var $location = false; //The location within your site you want to track
var $time = false; //This will just hold the current time
}
function __construct(){
//Here we will prepare data
}
?>
Step 5.
As you can see from the above code we have created a construct method, now we tell the class what to do when its first called
PHP:
<?php
class usersonline{
var $timeout = 7200; //this is the time you want to track your users back to
var $ip = false; // The ip address of the user
var $location = false; //The location within your site you want to track
var $time = false; //This will just hold the current time
}
function __construct(){
//Here we will prepare data
$this->time = time(); //$this->time is a reference to var $time..
}
?>
Step 6.
Ok the class should construct now and what we need to do is to create some methods/functions so we will begin with the function that gets the IP address.
PHP:
<?php
class usersonline{
var $timeout = 7200; //this is the time you want to track your users back to
var $ip = false; // The ip address of the user
var $location = false; //The location within your site you want to track
var $time = false; //This will just hold the current time
}
function __construct(){
//Here we will prepare data
$this->time = time(); //$this->time is a reference to var $time..
}
public function getIp(){
$this->ip = $_SERVER['REMOTE_ADDR'];
}
?>
Ok so when we run the getIp() function this will assign the var $ip with the IP address contained in $_SERVER
Step 7.
here we will create another function that will get the location of the user.
PHP:
<?php
class usersonline{
var $timeout = 7200; //this is the time you want to track your users back to
var $ip = false; // The ip address of the user
var $location = false; //The location within your site you want to track
var $time = false; //This will just hold the current time
}
function __construct(){
//Here we will prepare data
$this->time = time(); //$this->time is a reference to var $time..
}
public function getIp(){
$this->ip = $_SERVER['REMOTE_ADDR'];
}
public function getLocation(){
$this->location = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
//This should be something like (index.php?page=2)
}
?>
Step 8.
Ok now we have created all the methods/functions we need for the __constructor we can now update the constructor function
PHP:
<?php
class usersonline{
var $timeout = 7200; //this is the time you want to track your users back to
var $ip = false; // The ip address of the user
var $location = false; //The location within your site you want to track
var $time = false; //This will just hold the current time
}
function __construct(){
//Here we will prepare data
$this->time = time(); //$this->time is a reference to var $time..
//When we run what of the functions it will auto fill the Variables
$this->getIp();
$this->getLocation();
}
public function getIp(){
$this->ip = $_SERVER['REMOTE_ADDR'];
}
public function getLocation(){
$this->location = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
//This should be something like (index.php?page=2)
}
?>
Ok no when we instantiate the object it will automatically track the time,ip.location before we run any methods/functions
But below were going to create 2 primary functions that will furfill the main perpose of the script, they are:
- Update the MySql Table
- Return the count of users online
Step 9.1
Here were going to update the table with a method
PHP:
<?php
class usersonline{
var $timeout = 7200; //this is the time you want to track your users back to
var $ip = false; // The ip address of the user
var $location = false; //The location within your site you want to track
var $time = false; //This will just hold the current time
}
function __construct(){
//Here we will prepare data
$this->time = time(); //$this->time is a reference to var $time..
//When we run what of the functions it will auto fill the Variables
$this->getIp();
$this->getLocation();
}
public function getIp(){
$this->ip = $_SERVER['REMOTE_ADDR'];
}
public function getLocation(){
$this->location = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
//This should be something like (index.php?page=2)
}
public function update(){
$time_distance = ($this->timeout - $this->time);
$sql_query = "DELETE FROM useronline WHERE timestamp < ".$time_distance;
//Ok so now we have an sql string we will run the SQL
mysql_query($sql_query);
}
?>
Step 9.2
Ok so by now we have update the table and we want to be able to get the LATEST data from mysql witch will tell us how many UNIQUE users we have online
PHP:
<?php
class usersonline{
var $timeout = 7200; //this is the time you want to track your users back to
var $ip = false; // The ip address of the user
var $location = false; //The location within your site you want to track
var $time = false; //This will just hold the current time
}
function __construct(){
//Here we will prepare data
$this->time = time(); //$this->time is a reference to var $time..
//When we run what of the functions it will auto fill the Variables
$this->getIp();
$this->getLocation();
}
public function getIp(){
$this->ip = $_SERVER['REMOTE_ADDR'];
}
public function getLocation(){
$this->location = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
//This should be something like (index.php?page=2)
}
public function update(){
$time_distance = ($this->timeout - $this->time);
$sql_query = "DELETE FROM useronline WHERE timestamp < ".$time_distance;
//Ok so now we have an sql string we will run the SQL
mysql_query($sql_query);
}
function totalOnline(){
$count = @mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
}
?>
Ok so this function is basicaly calculating how many UNIQUE ip hits have been inserted into the data within the above timeframe.
Step 10
Ok so now the class is finally complete we can safe that into a php file now and name it usersonline.class.php and upload it to your server!
Step 11
Ok so now you want to get the count into your files, we will use the index file for example..
Open your index.php file or whatever file your adding this too and add the following block of code
PHP:
<?php
include 'path/to/usersonline.class.php';
/*
* :: Just a not you must be connected to the database before you call the class methods!!!
*/
//Assuming connected..
$usersonline = new usersonline;
/*
* Ok now as we run this line of code what ever
* was in the __construct will be auto run!
*/
//Ok we need to make sure that we run the update method to clean the amount of users up.
$usersonline->update(); // ok that should of cleaned tho rows up!
//Now we can add this to the out body for HTML
?>
<html>
<head>
<title>Uses online by Litewarez!</title>
</head>
<body>
<!-- your usual body here -->
<div class="usersonline">We have <?php echo $usersonline->totalOnline(); ?></div>
</body>
</html>
Ok now everything should be finished and i hope you enjoyed the tutorial.
P.S. Stay Warez