Iis log analysis program for PHP

Source: Internet
Author: User

/*************************************** ****************

* Function: Analyze iis logs, analyze the total number of IP addresses for access requests, and search engine captures

* Note:

* Place the log file in the root directory of the website and change it to log. log.

* Demo: http://www.zhanzhangpu.com/tools/iislog/

* Http://edu.ITbulo.com/UploadFiles_1485/200909/20090928132201681.gif

* Author: blackli, from the owner

* Problem: the search engine spider address is inaccurate, especially the Google spider address, which is a popular address column in China.

* The table has a large error and has limited capabilities. This problem cannot be solved. If you are interested, refer

* The URL below.

* Reference: http://www.seonewthing.com/GoogleBotCheck.aspx

* Http://googlewebmastercentral.blogspot.com/2006/09/how-to-verify-Googlebot.html

**************************************** ***************/

// Open the log file

$ DOCUMENT_ROOT = $ _ SERVER ['document _ root'];

$ Fp = fopen ("$ DOCUMENT_ROOT/log. log", 'rb ');

If (! $ Fp)

{

Echo 'failed to open the file ';

Exit;

}

// Analyze each line of logs

$ Num_ip = 0; // total number of access IP addresses

$ Ip = array (); // IP address data array. ip [$ I] [0] indicates the ip address, and ip [$ I] [1] indicates the number of times the IP address appears.

While (! Feof ($ fp ))

{

$ Line = fgets ($ fp, 1001 );

If (substr ($ line, 0, 1) = '#')

{

// Obtain the log generation time

If (substr ($ line, 0, 5) = '# date ')

{

$ Date_info = explode ('', $ line );

// Echo 'Log generation time: '. $ date_info [1].'. $ date_info [2]. '</br> ';

}

}

Else

{

// Obtain the access IP Address

If ($ line = '') continue;

$ Ip_info = explode ('', $ line );

For ($ j = $ num_ip-1, $ having_ip = false; $ j> = 0; $ j --)

{

If ($ ip [$ j] [0] = $ ip_info [6])

{

$ Having_ip = true;

$ Ip [$ j] [1] ++;

Break;

}

}

If ($ having_ip = false)

{

$ Ip [$ num_ip] [0] = $ ip_info [6];

$ Ip [$ num_ip] [1] = 1;

$ Num_ip ++;

}

}

}

// Retrieve search engine spider access times

// List of baidu and Google spider addresses

$ BaiduSpider = array ('192. 123', '192. 123', '192. 123', '61. 220 .');

$ GoogleBot = array ('74. 125. ',' 209. 85. ',' 66. 102. ',' 64. 233. ',' 64. 100', '123. 85. ');

$ Num_Spider = $ num_GoogleBot = 0;

For ($ I = 0; $ I <$ num_ip; $ I ++)

{

// Calculate the number of visits to Baidu spider

For ($ j = 0; $ j <4; $ j ++)

{

If (substr ($ ip [$ I] [0],) = $ baiduSpider [$ j])

{

$ Num_Spider + = $ ip [$ I] [1];

Continue;

}

}

// Calculate the number of Google spider visits

For ($ j = 0; $ j <6; $ j ++)

{

If (substr ($ ip [$ I] [0],) = $ GoogleBot [$ j])

{

$ Num_GoogleBot + = $ ip [$ I] [1];

Continue;

}

}

}

// Echo 'Baidu spider request'. $ num_Spider. 'Times <br/> ';

// Echo 'Google spider request'. $ num_GoogleBot. 'Times <br/> ';

Fclose ($ fp );

?>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> iis log analysis-Powered by ZhanZhangPu </title>

<Style type = "text/css">

# Main {

Width: 800px;

Margin: 0 auto;

Border: 1px solid # EEE;

}

# Main div {

Background: # EEE;

Border: 2px solid # FBFBFB;

Line-height: 25px;

Font-size: 15px;

Padding: 15px;

}

</Style>

</Head>

<Body>

<Div id = "main">

<H2> log generation time <? Php echo $ date_info [1]. ''. $ date_info [2];?> </H2>

<Div>

Total <? Php echo $ num_ip;?> IP request. <br/>

Baidu spider request <? Php echo $ num_Spider;?> Times. <br/>

Google spider request <? Php echo $ num_GoogleBot;?> Times.

</Div>

<Div>

IP address list: <br/>

<? Php

For ($ I = 0; $ I <$ num_ip; $ I ++)

{

Echo $ ip [$ I] [0]. ''. $ ip [$ I] [1]. 'times. <br/> ';

}

?>

</Div>

<Div> Powered by <a href = "http://www.zhanzhangpu.com"> webmaster shop </a> & trade; </div>

</Div>

</Body>

</Html>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.