In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use custom PHP applications to get Web server status information", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use custom PHP applications to get Web server status information" this article.
The structure of the common log file format (CommonLogfileFormat,CLF)
CLF was originally designed by NCSA for HTTPd (World wide Web server software). CERNHTTPd is a public domain Web server maintained by the World wide Web Consortium (WorldWideWebConsortium,W3C). The log file specification is listed on the W3C website. Both Microsoft and UNIX-based Web servers can generate log files in CLF format. The CLF format is as follows:
HostIdentAuthuserTime_Stamp "request" Status_codeFile_size
For example:
21.53.48.83 22/Apr/2002:22:19:12-[22/Apr/2002:22:19:12-0500] "GET/cnet.gifHTTP/1.0" 2008237
The following is a breakdown of log entries:
Host is the IP address or DNS name of the site visitor; in the above example, it is 21.53.48.83.
Ident is the remote identity (RFC931) of the visitor. The dash indicates "unspecified".
Authuser is the user ID (if the Web server has verified the identity of the visitors to the site).
Time_Stam is the time returned by the server in the format "day / month / year".
A Request is an HTTP request from a website visitor, such as GET or POST.
Status_Code is the status code returned by the server. For example, 200 represents "correct-browser request successful".
File_Size is the size of the file requested by the user. In this case, it is 8237 bytes.
Server status code
You can find the server status code specification developed by W3C in the HTTP standard. These status codes generated by the server indicate whether the data transfer between the browser and the server is successful. This code is generally passed to the browser (such as the famous 404 error "Page not found") or added to the server log.
collecting data
The first step in creating our custom application is to get user data. Every time a user selects a resource on the site, we want to create a corresponding log entry. Fortunately, the existence of server variables enables us to query the user's browser and get the data.
The server variable in the header carries the information passed 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:
Let's look at the code for our PHP application. First, we need to define the site resources we want to track and specify the file size:
/ / get the name of the file we want to record
$fileName= "cnet-banner.gif"
$fileSize= "92292"
You don't need to save these values to static variables. If you want to track many items, you can save them to an array or database. In this case, you may want to find each entry through an external link, as follows:
Where "123" represents the record corresponding to "cnet-banner.gif". Then, we query the user's browser through the server variable. So we get the data we need to add new entries to our log file:
How to use customized PHP applications to obtain the status information of Web servers
/ / get 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:sO")
$reqType=$_SERVER ['REQUEST_METHOD']
$servProtocol=$_SERVER ['SERVER_PROTOCOL']
StatusCode= "200"
Then we check to see if the server returns a null value (null). According to the CLF specification, null values should be replaced by dashes. In this way, the task of the next code block is to find a null value and replace it with a dash:
/ / add dashes to null values (according to specifications)
If ($host== "") {$host= "-";}
If ($ident== "") {$ident= "-";}
If ($auth== "") {$auth= "-";}
If ($reqType== "") {$reqType= "-";}
If ($servProtocol== "") {$servProtocol= "-";}
Once we have the necessary information, these values are organized into a format that conforms to the CLF specification:
/ / create a string in CLF format
$clfString=$host. ". $ident.". $auth. "[". $timeStamp. "]\". $reqType. "/". $fileName. ". $servProtocol."\ ". $statusCode.". $fileSize. "\ r\ n"
Create a custom log file
Now, the formatted data can be stored in our custom log file. First, we will create a file naming convention and write a method (function) to generate a new log file every day. In the example given in this article, each file starts with "weblog-", followed by a month / day / year date with a .log file extension. The .log extension generally refers to the server log file. (in fact, most log analyzers search for .log files.)
/ / name the log file with the current date
$logPath= ". / log/"
$logFile=$logPath. "weblog-" .date ("mdy"). ".log"
Now, we need to determine whether the current log file exists. If it exists, we add entries to it; otherwise, the application creates a new log file. (the creation of a new log file usually occurs when the date changes, because the file name changes.)
/ / check whether the log file already exists
If (file_exists ($logFile)) {
/ / if it exists, open the existing log file
$fileWrite=fopen ($logFile, "a");}
Else {
/ / otherwise, create a new log file
$fileWrite=fopen ($logFile, "w");}
If you receive a PermissionDenied error message while writing or appending files, please change the permissions of the destination log folder to allow write operations. The default permission for most Web servers is "readable and executable". You can use the CHMOD command or the FTP client to change the permissions of the folder.
Then, we create a file locking mechanism so that when two or more users access the log file at the same time, only one of them can write to the file:
/ / create a locking mechanism for file write operations
Flock ($fileWrite,LOCK_SH)
Finally, we write the contents of the entry:
/ / write CLF entry
Fwrite ($fileWrite,$clfString)
/ / unlock the file
Flock ($fileWrite,LOCK_UN)
/ / close the log file
Fclose ($fileWrite)
Processing log data
After the production of the system, the customer wants to get a detailed statistical analysis of the visitor data collected. Because all custom log files are organized in a standard format, any log analyzer can process them. Log Analyzer is a tool that analyzes large log files and produces pie charts, histograms, and other statistical graphs. Log analyzers are also used to collect data and synthesize information about which users visit your site, the number of hits, and so on.
Here are some of the more popular log analyzers:
WebTrends is a very good log analyzer for large-scale websites and enterprise 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 its reports.
Abide by the standard
We can easily extend the application to support other types of logging. This allows you to capture more data, such as browser type and referrer (referrer refers to the previous page linked to the current page). The lesson here is that following standards or conventions when you are programming will eventually simplify your work.
The above is all the contents of the article "how to use customized PHP applications to get Web server status information". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.