用PHP来写记数器(详细介绍)_php基础
PHP实例剖析:计数器
作者:Sucre_tiger
本款计数器用文本计数,没有用到数据库,可以实现如下功能:
利用一个文本文件实现多个页的计数减少服务器的I/O占用率在需要纪录的文件里,只需加入很少的几行代码
基本思路如下:
服务器程序从文本文件中读取该页被浏览的次数,(因为所有文件向服务器提出请求时,他们的环境变量REQUEST_URI都代表他来自于何处... ...,所以,以请求文件的环境变量REQUEST_URI来辨别到底是那一页正被浏览。),将这个次数加一储存,并在调用这页的用户的计算机上显示出来。
请先看我的数据文本中纪录的数据样本,(红色为浏览次数,其前面为相应的被浏览的文件)
Counter.dat/script/s2.php|3|/script/s1.php|11| /script/counter.php|5| /testhtml/s2.php|7|/testhtml/s3.php|6|
Now,Let's go!
counter.php
/* 定义储存数据的文本文件 */
$counterFile="counter.dat\";
/* 定义一个标记,用来辨别现在需纪录的数据是否已经文本数据中 */
$sign=False;
/* 将数据以数组的方式读入变量 $sounterData 备用, */
$counterData=file($counterFile);
/* 用count()函数计算共有多少个纪录 */
/* 用explode()函数把$counterData[$i]按符号\"|\"分开,并以数组的方式送回到变量$varArray里 */
/* 函数implode()与explode()刚刚相反,把数组$varArray以符号\"|\"连接起来送回到$counterData */
/* 还利用了环境变量$PATH_INFO
for($i=0;$i {
$varArray=explode(\"|\",$counterData[$i]);
if ($varArray[0]==$GLOBALS[\"REQUEST_URI\"])
{
$varArray[1]++;
print($varArray[1]);
$counterData[$i]=implode(\"|\", $varArray);
$sign=True;
/* 找到本纪录所在的位置后, 用break 退出循环 */
break;
}
}
/* 在这里,利用implode()这个函数的功能,将数据整理好了,一起写入文本文件中 */
/* 这样,对服务器的I/O占用就降到了最低点
$data=implode(\"\",$counterData);
/* 打开文本文件,将数据写入 */
$fp=fopen($counterFile,\"w\");
fputs($fp,$data);
/* 如果需要纪录的数据不在文本里,标志$sign= Flase, 那么就往文本里添加数据 */
if (!$sign) {fputs($fp,\"\\n\".$GLOBALS[\"REQUEST_URI\"].\"|\".\"1\".\"|\");
print(\"1\");
/* 关闭数据文件 */
fclose($fp);
?>
我们已经看到了这段程序的工作过程,也都知道了思路,但如果,每个文件里都这样写,岂不是太麻烦.
别慌! 我们还有PHP提供的强大的require()功能呢! 我们把counter.php写成函数,使用就方便许多了。那还等什么,下面就是你所要的函数:
counter.inc
function Counter()
{
$counterFile=\"/freespace/sucre/public_html/counter.dat\";
$counterData=file($counterFile);
$sign=False;
for($i=0;$i {
$varArray=explode(\"|\",$counterData[$i]);
if ($varArray[0]==$GLOBALS[\"REQUEST_URI\"])
{
$varArray[1]++;
print($varArray[1]);
$counterData[$i]=implode(\"|\", $varArray);
$sign=True; break;
}
}
$data=implode(\"\",$counterData);
$fp=fopen($counterFile,\"w\");
fputs($fp,$data);
if (!$sign)
{
fputs($fp,\"\\n\".$GLOBALS[\"REQUEST_URI\"].\"|\".\"1\".\"|\");
print(\"1\");
}
fclose($fp);
}
?>
最好的检验方法就是“实践”,好了来看我们怎样调用它,先看一个例子:
counterTest.php
require(\"counter.inc\");
?>
您是第 counter();?>位阅读者
您只需在要计数的HTML文件的文件头加入require()函数,引入counter()函数为homepage的一部分。在需要的地方加入 counter();?>就可以了。
还有几点要注意的问题:
1、 记录数据的文件一定要有“写”的权限,一般设成“666”即可,如果该文件存放在一个子目录下,则对这个“目录”也要有“写”的权限;
2、 我在调试过程中遇到这样一个问题,我将counter.inc和counter.dat放在一个子目录include下面,然后在不同的子目录下面用require()函数进行调用,格式如下: require("../include/counter.inc")
?>
可是总是出现“权限不够”的错误,如有高手请指教。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
