我有一個基本的在線訪客計數器。我從here得到它。它工作很好,但我有一個問題。我在網絡遊戲中使用它,並且我有多臺服務器。我用它作爲服務器在線櫃檯。我沒有通過iframe將計數器頁面添加到服務器,並且它非常好。PHP - 不要從index.php算起用戶
但問題是,我想在索引(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");?>
嗨艾琳,你有什麼變數設定這讓服務器知道用戶登錄? 你可以圍繞這個函數計數,只有當他們的狀態爲登錄時纔算數,而不僅僅是訪問。 'if($ login === true){$ visitors_online ='0'+ CountVisitors();}' – guyver4mk
@ guyver4mk不是真的。這是F2P遊戲,無需註冊或登錄。我有聊天,我可以檢查名字,但它不是玩遊戲的「必做」事情,所以它會非常混亂。 – Eren
啊,我明白了。你也可以試着把它放在服務器的URL上,這個腳本被調用的東西就像'if(strpos('visitors',$ _SERVER ['REQUEST_URI'])== false){#Count Function#}'Just將要搜索的字符串更改爲您要忽略的頁面 – guyver4mk