Jump to content

Contador de Utilizadores PHP


Recommended Posts

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:


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

Página Principal:

$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'];
} 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();
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 {
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;
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • 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.