使用PHP和MySQL的简单网页命中计数器代码

Website stats provide important information to a website owner about how the site is doing and how many people visit. A hit counter counts and displays how many people visit a webpage.

网站统计信息向网站所有者提供有关网站运行情况和访问人数的重要信息。 点击计数器会计数并显示访问网页的人数。

The code for a counter varies depending on the programming language used and the amount of information you want the counter to collect. If you, like many website owners, use PHP and MySQL with your website, you can generate a simple hit counter for your webpage using PHP and MySQL. The counter stores the hit totals in a MySQL database.

计数器的代码因使用的编程语言和您希望计数器收集的信息量而异。 如果您像许多网站所有者一样,在网站上使用PHP和MySQL,则可以使用PHP和MySQL为您的网页生成一个简单的计数器。 计数器将命中总数存储在MySQL数据库中。

代码 ( The Code  )

To get started, create a table to hold the counter statistics. Do that by executing this code:

首先,创建一个表来保存计数器统计信息。 通过执行以下代码来做到这一点:


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

The code creates a database table named counter with a single field also called counter, which stores the number of hits the site receives. It is set to start at 1, and the count increases by one each time the file is called. Then the new number is displayed. This process is accomplished with this PHP code:

该代码创建一个名为counter的数据库表,该表具有一个也称为counter的字段,该表存储该站点收到的命中数。 它设置为从1开始,并且每次调用文件时,计数都会增加一。 然后显示新号码。 此过程通过以下PHP代码完成:


<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
//Adds one to the counter
mysql_query("UPDATE counter SET counter = counter + 1");
//Retrieves the current count
$count = mysql_fetch_row(mysql_query("SELECT counter FROM counter"));
//Displays the count on your site
print "$count[0]";
?>

This simple hit counter doesn't give the website owner valuable information such as whether the visitor is a repeat visitor or a first-time visitor, the location of the visitor, which page was visited, or how much time the visitor spent on the page. For that, a more sophisticated analytics program is necessary.

这个简单的点击计数器不会为网站所有者提供有价值的信息,例如访问者是重复访问者还是首次访问者,访问者的位置,访问哪个页面或访问者在页面上花费了多少时间。 。 为此,需要更复杂的分析程序。

反码提示 ( Counter Code Tips )

Wanting to know the number of people who visit your site makes sense. When you are comfortable with the simple counter code, you can personalize the code in several ways to work better with your website and gather the information you seek.

想知道访问您网站的人数很有意义。 如果您对简单的反代码感到满意,则可以通过多种方式对代码进行个性化设置,以更好地与网站配合使用并收集所需的信息。

  • Customize the database, table, and code to include other information

    自定义数据库,表和代码以包括其他信息
  • Hold the counter in a separate file and retrieve it using include ()

    将计数器保存在一个单独的文件中,然后使用include()进行检索
  • Format the counter text using regular HTML around the include function

    在包含函数周围使用常规HTML格式设置计数器文本

  • Create different rows on the counter table for additional pages on your website

    在计数器表上为网站上的其他页面创建不同的行

翻译自: https://www.thoughtco.com/web-page-hit-counter-2693831

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值