PHP Example profiling: Counters

Source: Internet
Author: User
Tags count explode include ini php file php example require

This section of the counter with a text count, not used in the database, you can achieve the following functions:

Use a text file to implement multiple page counts
Reduce server's I/O occupancy rate
In a file that requires 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 him 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|

Good! Let's take a look at the php file

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 explode () function to $counterdata[$i] by symbol "|" Separate, 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 "|" Link up and 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 write counter.php as a function, it is not convenient to use.

To use the Require () feature, you must make the appropriate configuration in the php.ini.

You may refer to my configuration process:

(See my "Road of success" friends to pay attention, I made a small change to httpd.conf, I hope to control)

The related parts of the httpd.conf configuration are:

scriptalias/php4/"c:/php4/"
alias/script/"c:/php4/script/"
This is the configuration of the directory at the time of the PHP runtime.

In order not to confuse, ordinary *.php files, placed under C:\php4\script
For a while our program should be placed under C:\php4\script\include. A directory that specifically places include files.

So, how to configure the php.ini? This is simple:

Look for "include_path" in php.ini and change it to:

include_path = "C:\php4\script\include" can be

Counter.inc

<?php
function Counter ()
{
$counterFile = "C:\\php4\\script\\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);
}
?>

Put him under the c:\php4\script\include\, namely: C:\php4\script\include\counter.inc

Careful you must find that they are different: $counterFile = "C:\\php4\\script\\counter.dat"

Yes, for all files to be placed in one place, absolute paths must be provided. Unix is different.

All right, let's see how we call it, 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.



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.