dpymrcl269187540 2012-02-05 18:52
浏览 50

活动用户脚本,用户计数无法正常工作

i have written a script to output active users on my site....

part of this is counting unique ips in the log, as the array i use to split the lines / data unloads active users from the array list after 5 minutes.....

however the "3 online users now" count is not working properly.....

it kinda works.... when someone views a page, it says there is 1 user

lets say i view a page.... 1 visitor

then user 2 views a page .... 2 visitors

but if i then view another page, it displays 3 users.....

even though i use the same ip for both page requests....

here is my code

$data = file_get_contents('active-log.txt');

$break = "
";

$lines = explode($break, $data);

foreach ($lines as $key => $value) {

$active_ip[] = $lines[$key][1];

}

$active_ip_count = array_unique($active_ip);

$active_users = (count($active_ip_count));

$active_users is the variable i use to display how many unique visitors are online at one time

thanks in advance for anyone that can help me thanks

....

EDIT

.....

here is a sample of the log saved....

 1328469393|157.55.39.84|g-book
 1328469398|157.55.39.84|downloads
 1328469400|157.55.39.84|badger
 1328469404|157.55.39.84|home
 1328469408|157.55.39.84|boneyard-dogs

the first part is timestamp (to remove the line from array, if timestamp is older than 5 minutes... this works fine)

the second part is ip

third part is page viewed and the new line is created with

$lines[$key][1] is the variable for each ip in each line....

as im not exacly a php expert, when writing scripts, i test them heavily while developing, and each time i add a new line of script , i echo the data, to check its what i hope, to make sure i make no mistakes......

here is a section of code that i didnt paste as i didnt think it was necessary....

foreach($lines as $k=>$v) {

$lines[$k] = explode("|", $v); }

// echo $lines[0][0]; // now this is first array of first line .... line 2 / url would be - $lines[1][2]

this is in my code, straight after the line "$lines = explode($break, $data);" in my code

  • 写回答

1条回答 默认 最新

  • dongyi8416 2012-02-05 19:14
    关注

    Have you looked at the output of var_dump($active_ip) after the foreach loop ends? With this setup, I'm pretty sure $lines[$key][1] is simply the first character of the line you're dealing with, so that's not going to work well for a number of reasons. What does active-log.txt look like? Does it only contain IP addresses or user names, too? If it only contains IP addresses, consider using something like this:

    <?php
    $data = file('active-log.txt');
    $no_duplicate_ips = array_unique($data);
    $active_users = (count($no_duplicate_ips));
    ?>
    

    Edit:

    Right, that makes sense then. Try this:

    $data = file_get_contents('active-log.txt');
    $break = "
    "; //Note that it's generally a good idea to use PHP_EOL throughout your code, for greater cross-platform compatibility
    $lines = explode($break, $data);
    
    $exploded_data = array();
    $active_ips = array();
    
    foreach($lines as $v) {
        $exploded_data = explode("|", $v); 
        //Now check whether the timestamp is not > 5 min
        if(TIMESTAMP CHECK HERE) { 
             //OK, this one is not too old
             $active_ips[] = $exploded_data[1]; 
        }
    }
    
    $active_ip_count = array_unique($active_ip);
    $active_users = (count($active_ip_count));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程