Jump to content
joka503

Contador de Utilizadores PHP

Recommended Posts

joka503

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:

<?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

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;
}
}
}
?>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. 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.