dongmie3526 2013-09-23 12:45
浏览 31
已采纳

PHP只有Hit Counter?

So, I used to play with web development many years ago, and I'm a little rusty, but not THAT rusty!

I just made a full fledge video game page the other day using PHP variables to load different games into different size iframes. With that said, why on Earth can I not get a simple PHP hit counter to work? I have downloaded script after script after script, CHMOD'ed the txt file to 777, the whole 9. Does Chrome not support hit counters or something? It seems even the sites I visit that offer hit counters through them don't work on their demo pages!! What is the deal? I remember years ago, I copied about 10 very basic lines of code, saved it as a PHP file, uploaded it along with a blank txt file to a server, and bam, worked perfectly everytime. What has changed?

Here's the code I'm using. By the way. I tried adding this into my index.html, I also tried using it as a separate php file and calling on it with INCLUDE, everything. Nothing seems to work.

<?php

$open = fopen(“hits.txt”, “r+”);
$value = fgets($open);
$close = fclose($open);

$value++;

$open = fopen(“hits.txt”, “w+”);
fwrite($open, $value); // variable is not restated, bug fixed.
$close = fclose($open);

?>

and then where I want the results to be displayed, I have,

<?php echo $value; ?>

Any ideas?

  • 写回答

3条回答 默认 最新

  • douxi3432 2013-09-23 12:58
    关注

    You can use the following as a basic hit counter:

    <?php
        $counter = file_get_contents('./file') + 1;
        file_put_contents('./file', $counter);
    ?>
    

    You may want to implement some way of checking that it's not just one user refreshing the page... Something simple like:

    <?php
        session_start();
        if(empty($_SESSION['visited']){
            $counter = file_get_contents('./file') + 1;
            file_put_contents('./file', $counter);
        }
        $_SESSION['visited'] = TRUE;
    ?>
    

    Will check if the user has already visited the site in the same session and not increment the value if they have.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch