PHP code for writing text logs

Source: Internet
Author: User

Copy codeThe Code is as follows:
**
* Write a file
* @ Param string $ file path
* @ Param string $ str write content
* @ Param char $ mode write mode
*/
Function writeFile ($ file, $ str, $ mode = 'W ')
{
$ Oldmask = @ umask (0 );
$ Fp = @ fopen ($ file, $ mode );
@ Flock ($ fp, 3 );
If (! $ Fp)
{
Return false;
}
Else
{
@ Fwrite ($ fp, $ str );
@ Fclose ($ fp );
@ Umask ($ oldmask );
Return true;
}
}

Extended applications, such as recording the url content of each request
Copy codeThe Code is as follows:
Function writeGetUrlInfo ()
{
// Obtain the requester's address, client, request page, and parameters
$ RequestInformation = $ _ SERVER ['remote _ ADDR ']. ','. $ _ SERVER ['HTTP _ USER_AGENT ']. ', http: // '.20._server}'http_host'}.html entities ($ _ SERVER ['php _ SELF']). '? '. $ _ SERVER ['query _ string']. "\ n ";
$ FileName = RootPath. '/log /'. date ('Y-m-d '). '. log'; // The website root directory RootPath is in the configuration FILE define ('rootpath', substr (dirname (_ FILE __)));
WriteFile ($ fileName, $ requestInformation, 'A'); // append
}


Use file_put_contents ($ filename, $ data, FILE_APPEND); better

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.