Quantcast
Channel: Tópicos
Viewing all articles
Browse latest Browse all 14700

Contador de Utilizadores PHP

$
0
0
Eu quero criar um contador de utilizadores online por IP sendo que ele guarda na base de dados os dados do utilizador mas o problema está em mostrar os dados presentes na base de dados. Eu testei numa página em branco e o código apresentava direito o número de utilizadores online mas quando passava para a minha página "index" dá-me o seguinte erro:
http://prntscr.com/8lzyoy

Penso que seja de fácil resolução mas não consigo mesmo concluir. Será que me podiam ajudar?

Página Principal:
Código :
<?php
$host = "****"; // your MySQL host i.e. the server on which the database is, usually localhost
$user = "****"; // your MySQL username
$db = "****"; // the database to which you're trying to connect to
$fileLoc = $_SERVER['DOCUMENT_ROOT'] . '/passwords.txt';
if (file_exists($fileLoc)) {
$fh = fopen($fileLoc, 'r');
$jsonStr = fgets($fh);
$arr = json_decode($jsonStr, true);
$dbPass = $arr['default-password'];
fclose($fh);
} else {
die('no file found');
}
$conn = mysql_connect("$host", "$user", "$dbPass") or die ("Unable to connect to database.");
mysql_select_db("$db", $conn);
include_once ("usersOnline.class.php");
$visitors_online = new usersOnline();
if (count($visitors_online->error) == 0) {
if ($visitors_online->count_users() == 1) {
                 echo "There is " . $visitors_online->count_users() . " visitor online";
}
else {
                 echo "There are " . $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";
}
?>

Página Users Online
Código :
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;
}
}
}
?>

Viewing all articles
Browse latest Browse all 14700