简体   繁体   English

PHP页面计数器脚本

[英]PHP page counter script

I am new to php, obviously.. I have created a simple page counter for a webpage to monitor the number of views. 很显然,我是php的新手。我为网页创建了一个简单的页面计数器,以监视视图数。 I've set up a mySQL database with three columns (id, page, views). 我建立了一个包含三列(id,页面,视图)的mySQL数据库。 and have included the following script on the relative pages, HOWEVER.. instead of increasing the count by 1, it increases it by 2 every time and I have no idea why. 并在相对页面上包含以下脚本,但是..不是将计数增加1,而是每次将计数增加2,我不知道为什么。 Can anyone help? 有人可以帮忙吗?

   <?php
    $page= 'index';

    include('solrx_scripts.php');

    $sql="SELECT * FROM view_log WHERE page = 'index'";
    $result=mysql_query($sql)  or die(mysql_error()); 
    while ($row=mysql_fetch_array($result)){

        $previous = $row['views'];
    }
    $new_count = $previous + 1;

    mysql_query("UPDATE view_log SET views=$new_count WHERE id = 'index'");

    exit;
    ?>

Try doing the addition in SQL instead of PHP 尝试用SQL代替PHP进行加法

mysql_query("UPDATE view_log SET views=views+1 WHERE id = 'index'");

Bear in mind that search engine spiders will cause the view count to be updated also. 请记住,搜索引擎蜘蛛会导致视图计数也被更新。 Something like Google Analytics will give you far more reliable statistics. 诸如Google Analytics(分析)之类的东西将为您提供更加可靠的统计信息。

You just need one line for this : 您只需要一行:

mysql_query("UPDATE `view_log` SET `views` = `views` + 1 WHERE `id` = 'index'");

and please DON'T use mysql php functions, you will definetly have many problems in the future as it is deprecated 并且请不要使用mysql php函数,因为它已过时,将来您肯定会遇到很多问题

Make it simply like this no need to select if You always want just add 1 使其简单地像这样,无需选择是否总是需要添加1

$page= 'index';

include('solrx_scripts.php');

mysql_query("UPDATE view_log SET views=views+1 WHERE id = 'index'");

exit;

If it still increase by 2 it means there is something in solrx_scripts.php or before it. 如果仍然增加2,则意味着solrx_scripts.php中或之前有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM