PHP头条
热点:

php脚本解析nginx日记


php脚本解析nginx日志

nginx日志格式


access_log日志格式

log_format  main  '$server_name$remote_addr$remote_user[$time_local]"$request"'
                  '$status$body_bytes_sent"$http_referer"'
                  '"$http_user_agent""$http_x_forwarded_for"';

日志参数

server_name 	  	 : 虚拟主机的主机名称
remote_addr 		 : 远程客户端的ip地址
remote_user 	 	 : 远程客户端用户名称
time_local  		 : 访问的时间与时区
status    		 : 记录请求返回的http状态码
body_bytes_sent 	 : 发送给客户端的文件主体内容的大小
http_referer	         : 从哪个页面链接访问过来 
http_user_agent 	 : 客户端浏览器信息
http_x_forwarded_for     : 客户端的真实ip

日志分割符

使用特殊的不可打印字符^A(ctrl+v,ctrl+a)作为日志分割符

根据关键字过滤文件内容


需求

根据http的请求里是否有“weibo”这个关键字提取文件的内容

php代码

    /**
     * Description:按行读取文件内容进行过滤匹配
     *
     * @return array
     */
    function readFileContent ($filename)
    {
        $weibo_content = array();
        $fh = @fopen($filename, 'r');
        
        if ($fh) {
            while (! feof($fh)) {
                $row = fgets($fh, 4096);
                $row_arr = explode("", $row);
                if (isset($row_arr[3]) && preg_match('/weibo/', $row_arr[3])) {
                    $weibo_content[] = $row_arr;
                }
            }
        }
        fclose($fh);
        
        return $weibo_content;
    }



www.phpzy.comtrue/phprm/15500.htmlTechArticlephp脚本解析nginx日记 php脚本解析nginx日志 nginx日志式 access_log日志式 log_format main '$server_name$remote_addr$remote_user[$time_local]"$request"' '$status$body_bytes_sent"$http_referer"' '"$http_user_agent""$http_x_forw...

相关文章

相关频道:

PHP之友评论

今天推荐