喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

[北京]代朋友公司招聘PHP高级程序员多名

上一主题 下一主题
     
标题: [问题] php读取日志文件的方法  
 
zotong2006
新手上路
Rank: 1



UID 127435
精华 0
积分 33
帖子 19
金钱 33 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-6-9
状态 离线
php读取日志文件的方法

先谢谢您看这个帖子!

  我想写一个搜索引擎蜘蛛访问记录统计的程序,但当初通过判断HTTP_AGENT的方法已经找不到答案,在网上也没有相关的资料,因为在别的网站上没办法调用统计程序,js不行iframe也不行,因为蜘蛛不读。直接用<a></a>只是偶尔有效,所以我想用另一种方法去做这个程序--直接读iis日志



但不知道php函数中有没有哪个函数可以直接读取iis日志的,如果有请高手赐教。



我现在用的方法是通过fopen打开文件后,通过fgets读取每一行记录,然后再用stristr函数判断函数中是不是包括蜘蛛名称,但stristr函数只能从某一位置开始读取指定长度,因为日志文件每条记录长度都可能不一样,所以下一步我不知道如何读取了。我把把码帖上来,高手指教一下



<?php

$handle = @fopen("aaa.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);
  if(stristr($buffer,'Baiduspider+(+http://www.baidu.com/search/spider.htm)')){
         echo "<font color=red>百度:</font>".$buffer."<br/>";
   echo "<font color=red>爬行日期:</font>".substr($buffer,0,19)."<br/>";
  }elseif(stristr($buffer,'msnbot/1.1+(+http://search.msn.com/msnbot.htm)')){
   echo "<font color=red>MSN:</font>".$buffer."<br/>";
   echo "<font color=red>爬行日期:</font>".substr($buffer,0,19)."<br/>";
  }
    }
    fclose($handle);
}



日志文件内容如下:(将日志保存为aaa.txt即可)日志文件的规律就是每列之间用空格分隔



#Software: Microsoft Internet Information Services 6.0
#Version: 1.0
#Date: 2008-04-18 00:01:52
#Fields: date time s-sitename s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status
2008-04-18 00:01:52 W3SVC2028462860 210.51.7.188 GET /soft/sort08/down-3373.html - 80 - 65.55.104.16 msnbot/1.1+(+http://search.msn.com/msnbot.htm) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/style.css - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /index.html - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /inc/main.js - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/ubb.css - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/logo.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/bg.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /adfile/banner.js - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/m_bg.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/bg_top.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /adfile/ad1.htm - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /login.asp - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/top_home.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/top_gift.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/top_mail.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /adfile/top.js - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/topbgdown_r2_c1.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/member_enter.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/login.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/reg.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/class_ar.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /skin/default/h_arrow.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:37 W3SVC2028462860 210.51.7.188 GET /count.asp id=1 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /skin/default/body_background_r2.jpg - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /skin/default/title1_r2.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /inc/online.asp id=1&stat=http://www.lqyou.com/&Referer= 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /skin/default/search.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /soft/UploadPic/2007-11/20071127354678768.jpg - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /skin/default/dot.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /soft/UploadPic/2007-11/2007112612431245789.jpg - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /adfile/indexmid.js - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /adfile/ad3.htm - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /skin/default/110.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:38 W3SVC2028462860 210.51.7.188 GET /skin/default/headbg.gif - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:39 W3SVC2028462860 210.51.7.188 GET /adfile/footer.js - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:39 W3SVC2028462860 210.51.7.188 GET /count.asp - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:39 W3SVC2028462860 210.51.7.188 GET /favicon.ico - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 404 0 2
2008-04-18 00:02:39 W3SVC2028462860 210.51.7.188 GET /inc/online.asp id=0&stat=http://www.lqyou.com/&Referer= 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:39 W3SVC2028462860 210.51.7.188 GET /inc/online.asp id=1&stat=http://www.lqyou.com/&Referer= 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:39 W3SVC2028462860 210.51.7.188 GET /inc/online.asp id=0&stat=http://www.lqyou.com/&Referer= 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:39 W3SVC2028462860 210.51.7.188 GET /login.asp - 80 - 117.8.114.204 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1) 200 0 0
2008-04-18 00:02:44 W3SVC2028462860 210.51.7.188 GET /soft/sort02/down-3416.html - 80 - 65.55.104.16 msnbot/1.1+(+http://search.msn.com/msnbot.htm) 200 0 0
2008-04-18 00:02:47 W3SVC2028462860 210.51.7.188 GET /soft/sort02/down-3414.html - 80 - 65.55.104.16 msnbot/1.1+(+http://search.msn.com/msnbot.htm) 200 0 0
2008-04-18 00:02:48 W3SVC2028462860 210.51.7.188 GET /soft/sort01/down-3366.html - 80 - 65.55.104.16 msnbot/1.1+(+http://search.msn.com/msnbot.htm) 200 0 0
2008-04-18 00:02:49 W3SVC2028462860 210.51.7.188 GET /soft/sort01/down-3364.html - 80 - 65.55.104.16 msnbot/1.1+(+http://search.msn.com/msnbot.htm) 200 0 0
2008-04-18 00:02:51 W3SVC2028462860 210.51.7.188 GET /soft/sort03/down-3387.html - 80 - 65.55.104.16 msnbot/1.1+(+http://search.msn.com/msnbot.htm) 200 0 0
2008-04-18 00:02:52 W3SVC2028462860 210.51.7.188 GET /soft/sort05/down-3463.html - 80 - 65.55.104.16 msnbot/1.1+(+http://search.msn.com/msnbot.htm) 200 0 0
2008-04-18 00:04:57 W3SVC2028462860 210.51.7.188 POST /bbss/api/insenz.php - 80 - 58.215.87.100 - 404 0 3
2008-04-18 00:08:52 W3SVC2028462860 210.51.7.188 GET /soft/sort05/list5_1.html - 80 - 61.135.162.204 Baiduspider+(+http://www.baidu.com/search/spider.htm) 200 0 64
2008-04-18 00:09:33 W3SVC2028462860 210.51.7.188 GET /soft/sort09/list9_5.html - 80 - 61.135.162.204 Baiduspider+(+http://www.baidu.com/search/spider.htm) 200 0 0
2008-04-18 00:21:25 W3SVC2028462860 210.51.7.188 GET /thread.php fid-4.html 80 - 221.194.137.22 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1;+.NET+CLR+1.1.4322;+InfoPath.1;+Qihoo;+) 404 0 2
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/style.css - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /index.html - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /inc/main.js - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/ubb.css - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/logo.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/bg.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/bg_top.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /adfile/banner.js - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/m_bg.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /adfile/ad1.htm - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/top_gift.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/top_home.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /login.asp - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:25 W3SVC2028462860 210.51.7.188 GET /skin/default/top_mail.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:26 W3SVC2028462860 210.51.7.188 GET /skin/default/topbgdown_r2_c1.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:26 W3SVC2028462860 210.51.7.188 GET /adfile/top.js - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:26 W3SVC2028462860 210.51.7.188 GET /skin/default/member_enter.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-04-18 00:26:26 W3SVC2028462860 210.51.7.188 GET /skin/default/login.gif - 80 - 117.10.14.111 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) 200 0 0
2008-6-20 08:56 PM#1
查看资料  发短消息  顶部
 
phpxxx (phpxxx)
中级会员
Rank: 3Rank: 3
老会员



UID 27861
精华 0
积分 453
帖子 449
金钱 453 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2004-4-16
状态 离线
[推荐阅读] 杭州众全网络科技有限公司诚招PHP程序员
php文件处理性能并不好,写一个awk脚本不是更方便吗?
2008-6-20 09:49 PM#2
查看资料  发短消息  顶部
 
zotong2006
新手上路
Rank: 1



UID 127435
精华 0
积分 33
帖子 19
金钱 33 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-6-9
状态 离线
[推荐阅读] 一个mail()的问题
我是个初学者不知道awk是什么
2008-6-20 09:53 PM#3
查看资料  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.033877 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP