Use a custom PHP application to obtain the status information of the Web Server

Source: Internet
Author: User
Tags flock ftp client hosting companies website hosting

Most website hosting companies support customers' access to website statistics. However, you often feel that the status information generated by the server is incomplete. For example, if the web server with incorrect configuration does not recognize certain file types, these types of files will not appear in the status information. Fortunately, you can use PHP to customize status information collectionProgramIn this way, you can obtain the information you need.

Structure of Common logfile format (CLF)

CLF was initially designed for httpd (Global Network Server Software) by NCSA. CERN httpd is a public domain web server maintained by the World Wide Web Consortium (W3C. The W3C website lists the log file specifications. Both Microsoft and UNIX-based Web servers can generate log files in CLF format. The CLF format is as follows:
Host identauthusertime_stamp "request" status_codefile_size

For example:
21.53.48.83--[22/APR/2002: 22: 19: 12-0500] "Get/cnet.gif HTTP/1.0" 200 8237

The following is the log entry category:

Host is the IP address or DNS name of the website visitor. In the preceding example, host is 21.53.48.83.
The ident is the remote identity of the visitor (RFC 931 ). The dash indicates "not specified ".
Authuser is the user ID (if the web server has verified the identity of the website visitor ).
Time_stam is the time that the server returns in the format of "day/month/year.
A request is an HTTP request of a website visitor, such as get or post.
Status_code is the status returned by the server.CodeFor example, "200" indicates "correct-the browser request is successful ".
File_size is the size of the file requested by the user. In this example, It is 8237 bytes.

Server Status Code

You can find the server status code specifications developed by W3C in the HTTP standard. The status code generated by the server indicates whether the data transmission between the browser and the server is successful. These codes are generally passed to the browser (for example, the very famous 404 error "Page not found") or added to the server log.

Collect data

The first step in creating a custom application is to obtain user data. Whenever you select a website resource, we want to create a corresponding log entry. Fortunately, the existence of server variables allows us to query user browsers and obtain data.

The server variables in the header carry the information transmitted from the browser to the server. Remote_addr is an example of a server variable. This variable returns the user's IP Address:
Example output: 27.234.125.222

The following PHP code displays the IP address of the current user:
<? PHP echo $ _ server ['remote _ ADDR '];?>

Let's take a look at the code of our PHP application. First, we need to define the website resources we want to track and specify the file size:
// Obtain the name of the file we want to record
$ Filename = "cnet-banner.gif ";
$ Filesize = "92292 ";

You do not need to save these values to static variables. If you want to track many entries, you can save them to an array or database. In this case, you may want to find each entry through an external link, as shown below:
<A href = "weblogger. php? Bannerid = 123 "> </a>

The record corresponding to "201712310400000000cnet-banner.gif. Then, we use server variables to query the user's browser. In this way, we can obtain the data required to add new entries in our log file:
// Obtain the CLF information of the website Viewer
$ Host = $ _ server ['remote _ ADDR '];
$ Ident = $ _ server ['remote _ ident'];
$ Auth = $ _ server ['remote _ user'];
$ Timestamp = date ("d/M/Y: H: I: s o ");
$ Reqtype = $ _ server ['request _ method'];
$ Servprotocol = $ _ server ['server _ Protocol'];
$ Statuscode = "200 ";

Then, we check whether the server returns a null value ). According to the CLF specification, the null value should be replaced by a break number. In this way, the task of the next code block is to find a null value and replace it with a break number:
// Add a break number to the null value (according to the specification)
If ($ host = "") {$ host = "-";}
If ($ ident = "") {$ ident = "-";}
If ($ auth = "") {$ auth = "-";}
If ($ reqtype = "") {$ reqtype = "-";}
If ($ servprotocol = "") {$ servprotocol = "-";}

Once necessary information is obtained, these values are organized into a CLF compliant format:
// Create a string in CLF format
$ Clfstring = $ host. "". $ ident. "". $ Auth. "[". $ timestamp. "] \" ". $ reqtype. "/". $ filename. "". $ servprotocol. "\"". $ statuscode. "". $ filesize. "\ r \ n ";

Create a custom log file

Now, formatted data can be stored in our custom log files. First, we will create a file naming convention and compile a daily method (function) for generating a new log file ). In the example given in this article, each file starts with "weblog-" and is then a date represented by month/day/year. The file extension is. log .. The log extension generally indicates the server log file. (In fact, most log analyzer searches for. Log Files .)
// Use the current date to name the log file
$ Logpath = "./log /";
$ Logfile = $ logpath. "weblog-". date ("mdy"). ". log ";

Now, we need to determine whether the current log file exists. If it exists, we will add entries to it; otherwise, the application will create a new log file. (The New log file is generally created when the date is changed because the file name changes .)
// Check whether the log file already exists
If (file_exists ($ logfile )){
// If yes, open the existing Log File
$ Filewrite = fopen ($ logfile, "");}
Else {
// Otherwise, create a new Log File
$ Filewrite = fopen ($ logfile, "W ");}

If you receive the "Permission denied" error message when writing or appending a file, change the permission of the target log folder to allow write operations. The default permission of most Web servers is "readable and executable ". You can use the CHMOD command or the FTP client to change the folder permissions.

Then, we create a file lock mechanism so that when two or more users access the log file at the same time, only one of them can write the file:
// Create a file write Lock Mechanism
Flock ($ filewrite, lock_sh );

Finally, we write the content of the entry:
// Write CLF entries
Fwrite ($ filewrite, $ clfstring );
// Unlock the file
Flock ($ filewrite, lock_un );
// Close the log file
Fclose ($ filewrite );

Process Log Data

After the system is productized, the customer wants to obtain a detailed statistical analysis of the collected visitor data. Since all custom log files are organized in a standard format, any log analyzer can process them. A log analyzer is a tool that analyzes large log files and generates pie charts, histograms, and other statistical graphs. Log analyzer is also used to collect data and provide information on which users access your website and the number of clicks.

The following lists several popular log analyzers:

Webtrends is a very good log analyzer, which is suitable for large-scale websites and enterprise-level networks.
Analog is a popular free log analyzer.
Webalizer is a free analysis program. It can generate HTML reports so that most Web browsers can view their reports.

Compliance with standards

We can easily extend this application to support other types of logging. In this way, you can capture more data, such as the browser type and referrer (referrer refers to the previous webpage that is linked to the current webpage ). The experience here is that following the standards or conventions during your programming will eventually simplify your work.

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.