PHP writes text log implementation code. For more information, see.
The 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
The 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