通过JavaScript中的ip自动刷新在线用户数

Auto refresh numbers of user online by ip in JavaScript

本文关键字:刷新 在线 用户数 JavaScript 中的 ip 通过      更新时间:2023-09-26

请帮忙,我想在主页上的用户数量在线自动刷新。

我正在使用的脚本:

online.php

我在函数中创建:

function online_ip() {

这是您想要解释数字的文本。

$explain = 'User Online: ';
$explain_ip = '<br />Your IP: ';

添加在线号码。您可以将实际数字设置为0

$additions = 0;

这是以分钟为单位的刷新时间。

$timer = 10;

将保存所有数据的文件的名称。

$filename = 'configs/online.lst';

if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$space = " ";
$ip = get_client_ip();
$string = "$ip|$time'n";
$a = fopen("$filename", "a+");
fputs($a, $string);
fclose($a);

$timeout = time()-(60*$timer);
$all = "";
$i = 0;
$datei = file($filename);
for ($num = 0; $num < count($datei); $num++) {
    $pieces = explode("|",$datei[$num]);
    if ($pieces[1] > $timeout) {
        $all .= $pieces[0];
        $all .= ",";
    }
    $i++;
}
$all = substr($all,0,strlen($all)-1);
$arraypieces = explode(",",$all);
$useronline = count(array_flip(array_flip($arraypieces)));

显示$timeout 内活动的人数

 echo $explain, $useronline+$additions, $explain_ip, $ip;

删除

  $dell = "";
    for ($numm = 0; $numm < count($datei); $numm++) {
        $tiles = explode("|",$datei[$numm]);
        if ($tiles[1] > $timeout) {
            $dell .= "$tiles[0]|$tiles[1]";
        }
    }

 if (!$datei) $datei = dirname(__FILE__)."/$filename";
    $time = @time();
    $ip = get_client_ip();
    $string = "$dell";
    $a = fopen("$filename", "w+");
    fputs($a, $string);
    fclose($a);

终端功能:

}

在home_page.php 中

<div id="demo"></div>

我正在重构Javascript(不工作)

setInterval(myOn, 1000);
function myOn() {
    <?php include('online.php'); ?>
document.getElementById("demo").innerHTML = "<?php online_ip(); ?>"; 
};
setTimeout(myOn, 10000);

谢谢,@okolimar:

var myOnl = setInterval(myOn, 5000);
function myOn() {
$.get( "online.php", function( data ) {
  $( "#demo" ).html( data );
});
};
setTimeout(myOn, 5000);

您的函数myOn需要使用AJAX适当的php文件进行调用,该文件将包含您需要的信息。这不起作用,因为当您显示页面时,PHP部分只执行一次,请参阅检查器中的源代码。<?php online_ip(); ?>在服务器上执行,并在js文件中提供。

为了获得动态,你需要调用类似的东西:

$.get( "somefile.php", function( data ) {
  $( "#demo" ).html( data );
  alert( "Load was performed." );
});

在somefile.php中,您需要具有:

<?php 
   include('online.php'); 
   online_ip();
?>