Use PHP to generate your own LOG file

Source: Internet
Author: User


If your server does not allow you to read its LOG files, you have to stop and analyze your visitors? Make your own LOG file!
The only thing you need to do is to use PHP to calculate the actual number of clicks. There is no Error, and there is no Error like '2017 Not modified' and 'internal Server error. Your code will generate your own LOG file.


<? Php

/* User-Defined variables */
$ Logfile = "clf. log";/* write the LOG file there */
$ Timezone = "+ 0100";/* Timezone correction */
$ Lookup_size = true;/* set file permissions */
$ Document_root = "/usr/local/apache/share/htdocs ";

/* He or she may not count the same client
* You must set the variable $ document_root to work.
*/

Function write_to_log ($ str ){
If ($ fd = @ fopen ($ GLOBALS ["logfile"], "")){
Fputs ($ fd, $ str );
Fclose ($ fd );
}
}

Function get_var ($ name, $ default ){
If ($ var = getenv ($ name )){
Return $ var;
} Else {
Return $ default;
}
}

If ($ remote_host = get_var ("REMOTE_HOST", false )){
$ Remote_host = get_var ("REMOTE_ADDR ","-");
}
$ Remote_user = get_var ("REMOTE_USER ","-");
$ Remote_ident = get_var ("REMOTE_IDENT ","-");
$ Server_port = get_var ("SERVER_PORT", 80 );
If ($ server_port! = 80 ){
$ Server_port = ":". $ server_port;
} Else {
$ Server_port = "";
}
$ Server_name = get_var ("SERVER_NAME ","-");
$ Request_method = get_var ("REQUEST_METHOD", "GET ");
$ Request_uri = get_var ("REQUEST_URI ","");
$ User_agent = get_var ("HTTP_USER_AGENT ","");
If ($ lookup_size = true & $ document_root ){
$ Filename = ereg_replace ("\?. * "," ", $ Request_uri );
$ Filename = "$ document_root $ filename ";
If (! $ Size = filesize ($ filename )){
$ Size = 0;
}
} Else {
$ Size = 0;
}

$ Date = gmdate ("d/M/Y: H: I: s ");
$ Log = "$ remote_host $ remote_ident $ remote_user [$ date $ timezone] \".
"$ Request_method http: // $ server_name $ server_port $ request_uri \" 200 $ size \ n ";

Write_to_log ($ log );

?>

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.