Thursday, May 31, 2012

Users online php

Users online php

You have created my_database in your previous lessens.Now you have to create table called "useronline" in your my_database using following fields.

CREATE TABLE `useronline` (
  `id` int(10) NOT NULL auto_increment,
  `ip` varchar(15) NOT NULL default '',
  `timestamp` varchar(15) NOT NULL default '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id`(`id`)
) TYPE=MyISAM COMMENT='' AUTO_INCREMENT=1 ;

This page shows the number of users online

 <?php
 include_once ("usersOnline.php");
$visitors_online = new usersOnline();

if (count($visitors_online->error) == 0) {

    if ($visitors_online->count_users() == 1) {
        echo "<table bgcolor=#515fb0><tr bgcolor=#ffffff><td>There is " . $visitors_online->count_users() . " visitor online</td></tr></table>";
    }
    else {
        echo "<table bgcolor=#515fb0><tr bgcolor=#ffffff><td>There are " . $visitors_online->count_users() . " visitors online</td></tr></table>";
    }
}
else {
    echo "<b>Users online class errors:</b><br /><ul>\r\n";
    for ($i = 0; $i < count($visitors_online->error); $i ++ ) {
        echo "<table bgcolor=#515fb0><tr bgcolor=#ffffff><td>" . $visitors_online->error[$i] . "visitor online</td></tr></table>";
    }
}
?>

Bellow shows you usersOnline.php which connect with your data base and insert the user details.

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "my_database";
$conn = mysql_connect("$host","$user","$pass") or die ("Unable to connect to database.");
mysql_select_db("$db", $conn);
class usersOnline {

    var $timeout = 600;
    var $count = 0;
    var $error;
    var $i = 0;
   
    function usersOnline () {
        $this->timestamp = time();
        $this->ip = $this->ipCheck();
        $this->new_user();
        $this->delete_user();
        $this->count_users();
    }
   
    function ipCheck() {
   
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_X_FORWARDED')) {
            $ip = getenv('HTTP_X_FORWARDED');
        }
        elseif (getenv('HTTP_FORWARDED_FOR')) {
            $ip = getenv('HTTP_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_FORWARDED')) {
            $ip = getenv('HTTP_FORWARDED');
        }
        else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
   
    function new_user() {
        $insert = mysql_query ("INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
        if (!$insert) {
            $this->error[$this->i] = "Unable to record new visitor\r\n";           
            $this->i ++;
        }
    }
   
    function delete_user() {
        $delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
        if (!$delete) {
            $this->error[$this->i] = "Unable to delete visitors";
            $this->i ++;
        }
    }
   
    function count_users() {
        if (count($this->error) == 0) {
            $count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
            return $count;
        }
    }

}

?>

No comments:

Post a Comment