Jump to content

modify this class to save the numbers of users online instead of deleting.


co.ador

Recommended Posts

Below in page1.php is the php class in charge of inserting and then deleting the users online right after the time is out. Now i would like to modify this class that instead of deleting the users to also save for personal references in a table apart the amount of users of a x day...

 

Can any body display some idea on how to go about that?

 

Thank you.

the mysql table set up goes as

 

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 ;

page1.php

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() {
/*
This function will try to find out if user is coming behind proxy server. Why is this important?
If you have high traffic web site, it might happen that you receive lot of traffic
from the same proxy server (like AOL). In that case, the script would count them all as 1 user.
This function tryes to get real IP address.
Note that getenv() function doesn't work when PHP is running as ISAPI module
*/
	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;
	}
}

}

?>

 

 

The instantiation in page2.php will online display the users online.

 

page2.php

<?php 

$visitors_online = new usersOnline(); 

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

    if ($visitors_online->count_users() == 1) { 
        echo   $visitors_online->count_users() . " visitor online"; 
    } 
    else { 
        echo   $visitors_online->count_users() . " visitors online"; 
    } 
} 
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"; 

} 
?>

Below in page1.php is the php class in charge of inserting and then deleting the users online right after the time is out. Now i would like to modify this class that instead of deleting the users to also save for personal references in a table apart the amount of users of a x day...

 

Can any body display some idea on how to go about that?

 

Thank you.

the mysql table set up goes as

 

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 ;

page1.php

<?php
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() {
/*
This function will try to find out if user is coming behind proxy server. Why is this important?
If you have high traffic web site, it might happen that you receive lot of traffic
from the same proxy server (like AOL). In that case, the script would count them all as 1 user.
This function tryes to get real IP address.
Note that getenv() function doesn't work when PHP is running as ISAPI module
*/
	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;
	}
}

}

?>

 

 

The instantiation in page2.php will online display the users online.

 

page2.php

<?php 

$visitors_online = new usersOnline(); 

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

    if ($visitors_online->count_users() == 1) { 
        echo   $visitors_online->count_users() . " visitor online"; 
    } 
    else { 
        echo   $visitors_online->count_users() . " visitors online"; 
    } 
} 
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"; 

} 
?>

 

Sorry to repeat the post again it's just that i have miss the php tags

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.