2015-09-07 27 views
1

我有一个基本的在线访客计数器。我从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");?> 
+0

嗨艾琳,你有什么变数设定这让服务器知道用户登录? 你可以围绕这个函数计数,只有当他们的状态为登录时才算数,而不仅仅是访问。 'if($ login === true){$ visitors_online ='0'+ CountVisitors();}' – guyver4mk

+0

@ guyver4mk不是真的。这是F2P游戏,无需注册或登录。我有聊天,我可以检查名字,但它不是玩游戏的“必做”事情,所以它会非常混乱。 – Eren

+0

啊,我明白了。你也可以试着把它放在服务器的URL上,这个脚本被调用的东西就像'if(strpos('visitors',$ _SERVER ['REQUEST_URI'])== false){#Count Function#}'Just将要搜索的字符串更改为您要忽略的页面 – guyver4mk

回答

1

make a server2.php wi TH这代码

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

+0

这是非常好的解决方案。我有7台服务器,但我想没有选择。谢谢! – Eren