Use PHP to write registers (detailed description)

Source: Internet
Author: User
Tags count explode php example require
PHP Example profiling: Counters
Author: Sucre_tiger
This section of the counter with a text count, not used in the database, you can achieve the following functions:
Using a text file to implement multiple page counts reduce server I/O occupancy rate in files that require a record, just add a few lines of code
The basic ideas are as follows:
The server program reads the number of times the page has been browsed from a text file. (Because all files request to the server, their environment variable Request_uri represents where he came from ..., so, to request a file's environment variable Request_uri to identify exactly which page is being browsed.) , add this number to a store and display it on the computer of the user who called the page.
Please look at the data sample recorded in my data text, (red is the browse times, the corresponding browsed file before it)
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
<title> counter </title>
<body>
<?php
/* Defines a text file for storing data * *
$counterFile = "counter.dat\";
/* Defines a tag that identifies whether the data currently required for recording is already in the text data.
$sign =false;
/* Read the data in an array as a variable $sounterData standby, * *
$counterData =file ($counterFile);
/* Use the count () function to calculate how many records are in total * *
/* Use the explode () function to separate $counterdata[$i] by symbol \ "|\" and send it back to the variable $vararray in the form of an array.
/* function implode () and explode () just opposite, the array $vararray with the symbol \ "|\" connected up to send back to $counterdata * *
/* Also utilizes the environment variable $path_info
For ($i =0 $i <=count ($counterData)-1; $i + +)
{
$varArray =explode (\ "|\", $counterData [$i]);
if ($varArray [0]== $GLOBALS [\ request_uri\])
{
$varArray [1]++;
Print ($varArray [1]);
$counterData [$i]=implode (\ "|\", $varArray);
$sign =true;
* * Find the location of this record, use the break to exit the cycle * *
Break
}
}
/* Here, using the function of implode (), the data is sorted and written together in a text file.
/* This will reduce the I/O footprint of the server to the lowest point
$data =implode (\ "\", $counterData);
/* Open Text file, write data to * *
$FP =fopen ($counterFile, \ "w\");
Fputs ($fp, $data);
* * If you need to record the data is not in the text, the logo $sign= flase, then add the data to the text * *
if (! $sign) {fputs ($fp, \ "\\n\" $GLOBALS [\ "Request_uri\"].\ "|\". \ "1\". \ "|\");
Print (\ "1\");
/* Close Data File * *
Fclose ($FP);
?>
</body>
We have seen the process of this procedure, but also know the idea, but if it is written in each file, it is too troublesome.
Don't panic! We also have the powerful require () function that PHP provides! We use counter.php as a function, which is much more convenient. That's what you're waiting for, and here's the function you want:
Counter.inc
<?php
function Counter ()
{
$counterFile =\ "/freespace/sucre/public_html/counter.dat\";
$counterData =file ($counterFile);
$sign =false;
For ($i =0 $i <=count ($counterData)-1; $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);
}
?>
The best way to test this is "practice," so let's see how we call it, and first look at one example:
countertest.php
<?php
Require (\ "counter.inc\");
?>
<title> Web Counter final version </title>
<body>
You are the first;? Counter ();? > Reader
</body>
You simply add the require () function to the file header of the HTML file you want to count, and introduce the counter () function as part of the homepage. Join in where you need to be Counter ();? > is OK.
There are a few things to note:
1, record the data file must have "write" permission, generally set to "666" can be, if the file is stored in a subdirectory, then the "directory" must have "write" permission;
2, I encountered in the debugging process such a problem, I put counter.inc and Counter.dat in a subdirectory include below, and then under different subdirectories with the Require () function to invoke, the format is as follows: <?php
Require (".. /include/counter.inc ")
?>
But always appear "insufficient authority" error, such as a master please advise.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.