1

基本的なオンライン ビジター カウンターがあります。ここから取得します。非常にうまく機能しますが、問題があります。オンラインゲームで使用しており、複数のサーバーがあります。サーバーのオンラインカウンターに使用しています。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 インクルード (index.php)

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

1 に答える 1

1

このコードで 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のように使用します

于 2015-09-07T12:51:06.250 に答える