使用 PHP 和 MySQL 的页面浏览量计数器? [英] Page Views counter using PHP and MySQL?

查看:47
本文介绍了使用 PHP 和 MySQL 的页面浏览量计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何更改我的代码,以便在存在多个页面时计算每个页面的页面浏览量并为每个页面显示正确的页面浏览量.

I was wondering how can I alter my code so it can count the page views for each page when multiple pages are present and display the correct page views for each page.

下面是我的php代码.

Here is my php code below.

//Adds one to the counter
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"UPDATE counter SET counter = counter + 1");

//Retreives the current count
$count = mysqli_fetch_row(mysqli_query($mysqli,"SELECT counter FROM counter"));

if (!$dbc) 
{
    // There was an error...do something about it here...
    print mysqli_error();
}

这是我的 MySQL 代码.

And here is my MySQL code.

CREATE TABLE `counter` ( `counter` INT( 20 ) NOT NULL );
INSERT INTO counter VALUES (0); 

这是我之前的备用桌.

CREATE TABLE `counter` (
  `id` int(11) NOT NULL auto_increment,
  `ip` varchar(15) NOT NULL default '',
  `page` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`),
 KEY `ip` (`ip`,`page`)
)

这是我的旧页面计数器,我正在尝试将它与我的新页面计数器结合起来.

This was my old page counter which I'm trying to combine with my new page counter.

//Get the viewer's IP Address and the page he / she is viewing
$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['PHP_SELF'];

//Check that the IP is not already listed for the current page
$viewer_check_query = "SELECT * FROM counter WHERE ip = '$ip' AND page = '$page'";
$viewer_check_result = mysql_query($viewer_check_query);
$viewer_check_numrows = mysql_num_rows($viewer_check_result);
    //If numrows is equal to zero, then the user is new
    if($viewer_check_numrows == 0){
        //Add the new entry
        $viewer_new_query = "INSERT INTO counter (ip, page) VALUES
        ('$ip', '$page')";
        $viewer_new_result = mysql_query($viewer_new_query);
    }

//Get the total number of viewers for this page
$viewer_total_query = "SELECT * FROM counter WHERE page = '$page'";
$viewer_total_result = mysql_query($viewer_total_query);
$viewer_total_numrows = mysql_num_rows($viewer_total_result);

推荐答案

我不建议您实时"更新数据库.而是使用 mod_log_mysql 或离线"解析 Apache 日志文件.

I would not recommend you to update the database "live". But rather use mod_log_mysql or parse the Apache logfile "offline".

为什么不呢?您可能想知道,如果有一天您有很多访问者,页面加载会停止,直到数据库更新.或者,如果数据库负载过重,更新会导致网页加载停止.

Why not? You might wonder, if you have a lot of visitors some day the page load would stall until the database has been updated. Or if there is heavy load on the database the update would stall the loading of the webpage.

还有一个提示,使用 jQuery 或 javascript 将一个特殊的日志事件"写入您的 apache 日志,这样您就不会记录所有爬虫等.我正在使用以下代码:

And one more tip, use jQuery or javascript to write a special "logevent" to your apache log so you won't log all crawlers etc. I'm using the following code:

 function logger() {
  var currenturl = location.href;
  $.get("/logthis", { url: currenturl } );
}
$(document).ready(function(){
   logger();
});

这篇关于使用 PHP 和 MySQL 的页面浏览量计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆