当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP 写文本日志实现代码

PHP 写文本日志实现代码

2019年04月29日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

**
* 写文件
* @param string $file 文件路径
* @param string $str 写入内容
* @param char $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;
}
}

扩展应用,比如记录每次请求的url内容
复制代码 代码如下:

function writegeturlinfo()
{
  //获取请求方的地址,客户端,请求的页面及参数
   $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'; //网站根目录rootpath是在配置文件里define('rootpath', substr(dirname(__file__)));
  writefile($filename, $requestinformation, 'a'); //表示追加
}


用file_put_contents($filename,$data,file_append);更佳

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网