Home >Backend Development >PHP Tutorial >PHP writing text log implementation code_PHP tutorial

PHP writing text log implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:37:34872browse

Copy code The code is as follows:

**
* Write file
* @param string $file file path
* @param string $str writing content
* @param char $mode writing 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 the code The code is as follows:

function writeGetUrlInfo()
{
  // Get the requester’s address, client, requested page and parameters
$requestInformation = $_SERVER['REMOTE_ADDR'].', '.$_SERVER['HTTP_USER_AGENT'].', http://'.$_SERVER['HTTP_HOST'].htmlentities     ($_SERVER['PHP_SELF']). '?'.$_SERVER['QUERY_STRING']."n";
 $fileName = RootPath.'/log/'.date('Y-m-d').'.log'; //Website root directory RootPath It is in the configuration file define('RootPath', substr(dirname(__FILE__)));
 writeFile($fileName, $requestInformation, 'a'); //Indicates appending
}


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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321895.htmlTechArticleCopy code The code is as follows: ** *Write file* @param string $file File path* @param string $str Write content* @param char $mode writing mode*/ function writeFile($file,$str,$mo...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn