[NEED] Amount On Website Reader

Status
Not open for further replies.

wite

Posting Freak
Aug 3, 2012
593
131
Hi,

I was wondering if there was a way that my website could pick up the amount of people that have it open and then say the amount on the website in a specific place.
If someone has the code please tell me!

Thanks,
Supa.
 

Mee

Member
May 4, 2012
232
28
Here is a code that shows how many people are online, made by me and I used it for my fansite, tested it and it works great. Lost the MySQL database import file thingo but I'm sure you'll know what to do.

usersOnline.class.php

Code:
<?php
$host = "";
$user = "";
$pass = "";
$db = "";
 
$conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database."); 
mysql_select_db("$db", $conn);
 
class usersOnline {
 
var $timeout = 100;
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;
}
}
 
}
 
?>

Display code:

Code:
<?php 
 include_once ("usersOnline.class.php"); 
$visitors_online = new usersOnline(); 
 
if (count($visitors_online->error) == 0) { 
 
    if ($visitors_online->count_users() == 1) { 
        echo "<b>Total Online Visitor(s):</b> " . $visitors_online->count_users() . ""; 
    } 
    else { 
        echo "<b>Total Online Visitor(s):</b> " . $visitors_online->count_users() . ""; 
    } 
} 
else { 
    echo "<b>Users online class errors:</b><br /><ul>\r\n"; 
    for ($i = 0; $i < count($visitors_online->error); $i ++ ) { 
        echo "<li>" . $visitors_online->error[$i] . "</li>\r\n"; 
    } 
    echo "</ul>\r\n"; 
 
} 
?>
 

wite

Posting Freak
Aug 3, 2012
593
131
Here is a code that shows how many people are online, made by me and I used it for my fansite, tested it and it works great. Lost the MySQL database import file thingo but I'm sure you'll know what to do.

usersOnline.class.php

Code:
<?php
$host = "";
$user = "";
$pass = "";
$db = "";
 
$conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database.");
mysql_select_db("$db", $conn);
 
class usersOnline {
 
var $timeout = 100;
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;
}
}
 
}
 
?>

Display code:

Code:
<?php
include_once ("usersOnline.class.php");
$visitors_online = new usersOnline();
 
if (count($visitors_online->error) == 0) {
 
    if ($visitors_online->count_users() == 1) {
        echo "<b>Total Online Visitor(s):</b> " . $visitors_online->count_users() . "";
    }
    else {
        echo "<b>Total Online Visitor(s):</b> " . $visitors_online->count_users() . "";
    }
}
else {
    echo "<b>Users online class errors:</b><br /><ul>\r\n";
    for ($i = 0; $i < count($visitors_online->error); $i ++ ) {
        echo "<li>" . $visitors_online->error[$i] . "</li>\r\n";
    }
    echo "</ul>\r\n";
 
}
?>
Thanks for that.
But what will the database need to be called?
 
Status
Not open for further replies.

Users who are viewing this thread

Top