繁体   English   中英

PHP-不计算index.php中的用户

[英]PHP - Don't Count Users from index.php

我有一个基本的在线访客计数器。 我从这里得到的。 它运行很好,但是我有一个问题。 我在在线游戏上使用它,并且有多个服务器。 我将其用于服务器在线计数器。 我确实通过iframe将计数器页面添加到服务器,并且计数非常好。

但是问题是,我想在索引(index.php)页面上显示该数字。 但是当我使用时:

<?php include("server1.php");?>

它也计算索引页面用户。 我不要这个 如何使它不计算来自index.php的IP?

在这里,我的密码

计数器(server1.php)

<?php
$dbfile = "game/database/1.db";  // path to data file
$expire = 100; // average time in seconds to consider someone online before removing from the list

if(!file_exists($dbfile)) {
    die("Error: Data file " . $dbfile . " NOT FOUND!");
}

if(!is_writable($dbfile)) {
    die("Error: Data file " . $dbfile . " is NOT writable! Please CHMOD it to 666!");
}

function CountVisitors() {
    global $dbfile, $expire;
    $cur_ip = getIP();
    $cur_time = time();
    $dbary_new = array();

    $dbary = unserialize(file_get_contents($dbfile));
    if(is_array($dbary)) {
        while(list($user_ip, $user_time) = each($dbary)) {
            if(($user_ip != $cur_ip) && (($user_time + $expire) > $cur_time)) {
                $dbary_new[$user_ip] = $user_time;
            }
        }
    }
    $dbary_new[$cur_ip] = $cur_time; // add record for current user

    $fp = fopen($dbfile, "w");
    fputs($fp, serialize($dbary_new));
    fclose($fp);

    $out = sprintf("%03d", count($dbary_new)); // format the result to display 3 digits with leading 0's
    return $out;
}

function getIP() {
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    elseif(isset($_SERVER['REMOTE_ADDR'])) $ip = $_SERVER['REMOTE_ADDR'];
    else $ip = "0";
    return $ip;
}

$visitors_online = '0'+CountVisitors();
?>

<?=$visitors_online;?>

iframe(我在服务器页面上使用它)

<iframe name="visitors" src="../1.php" width="1" hidden="true" height="1" frameborder="0" scrolling="no"></iframe>

php include(index.php)

Server 7 - Online Players:<?php include("7.php");?>

用此代码制作server2.php

<?php
$dbfile = "game/database/1.db";  // path to data file
$expire = 100;

if(!file_exists($dbfile)) {
    die("Error: Data file " . $dbfile . " NOT FOUND!");
}

if(!is_writable($dbfile)) {
    die("Error: Data file " . $dbfile . " is NOT writable! Please CHMOD it to 666!");
}

function CountVisitors() {
    global $dbfile, $expire;
    $cur_time = time();
    $dbary_new = array();

    $dbary = unserialize(file_get_contents($dbfile));
    if(is_array($dbary)) {
        while(list($user_ip, $user_time) = each($dbary)) {
            if(($user_time + $expire) > $cur_time) {
                $dbary_new[$user_ip] = $user_time;
            }
        }
    }

    $out = sprintf("%03d", count($dbary_new)); // format the result to display 3 digits with leading 0's
    return $out;
}

$visitors_online = '0'+CountVisitors();
?>

<?=$visitors_online;?>

并像server1.php一样使用它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM