12c07-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 176 行
PHP
176 行
<?php// Declare some basic variables$matches = array();$names = array();// Open the logfile for reading - Modify this to open your own file // in 'combined' log format.if (!($fp = @fopen('access.log', 'r'))) { die("File open failed for the access log");}// Now loop through the file reading one line at a time:while ($logline = fgets($fp)) { // Parse the logfile line: if (preg_match('/^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]+)" (\S+)' . ' (\S+) "([^"]*)" "([^"]*)"$/', $logline, $matches)) { // Save the agent type, and prepare for finding OS type: $agent = $matches[9]; $ostype = ''; // Loop through all choices checking against the OS string if (strpos($agent, 'Win') !== false) { $ostype = 'Windows'; } elseif (strpos($agent, 'Mac') !== false) { $ostype = 'Macintosh'; } elseif (preg_match('/X11|Linux/', $agent)) { $ostype = 'Unix'; } else { $ostype = 'Other OS'; } // Go through all choices checking against the agents // The order is important, so that 'greedier' comparisons later // don't grab too much. $results = array(); // Find IE if (preg_match('/MSIE ([0-9.]+)/', $agent, $results)) { @$names["IE {$results[1]} - $ostype"]['count']++; // Safari } elseif (preg_match('/Safari\/([0-9.]+)/', $agent, $results)) { @$names["Safari {$results[1]}"]['count']++; @$names["Safari {$results[1]}"]['subs'][$ostype]++; // Konqueror } elseif (preg_match('/Konqueror\/([0-9.]+)/',$agent, $results)) { @$names["Konqueror {$results[1]}"]['count']++; @$names["Konqueror {$results[1]}"]['subs'][$ostype]++; // Opera } elseif (preg_match('/Opera\/?([0-9.]+)/',$agent, $results)) { @$names["Opera {$results[1]}"]['count']++; @$names["Opera {$results[1]}"]['subs'][$ostype]++; // Mozilla, modern with a rv statement } elseif (preg_match('/rv:([a-z0-9.]+)/',$agent, $results)) { @$names["Mozilla {$results[1]}"]['count']++; // See WHAT version of Mozilla this really is $spec = array(); if (preg_match('/Netscape6?\/([0-9.]+)/',$agent, $spec)) { @$names["Mozilla {$results[1]}"]['subs'] ["Netscape {$spec[1]} - {$ostype}"]++; } elseif (preg_match('/(?:Firefox|Firebird|Phoenix)\/([0-9.]+)/', $agent, $spec)) { @$names["Mozilla {$results[1]}"]['subs'] ["Firefox {$spec[1]} - {$ostype}"]++; } else { @$names["Mozilla {$results[1]}"]['subs'] ["Mozilla {$results[1]} - {$ostype}"]++; } // Old Netscape } elseif (preg_match('/Mozilla\/([0-9.]+)/',$agent, $results)) { @$names["Netscape {$results[1]}"]['count']++; @$names["Netscape {$results[1]}"]['subs'][$ostype]++; // Some grabbing agents that we regularly see. } elseif (preg_match('/[Gg]oogle|Java|msn/', $agent)) { @$names['Known non-browsers']['count']++; // Well, everything else is an 'other' } else { @$names['Other Browsers']['count']++; @$names['Other Browsers']['subs'][$ostype]++; } }}// Clean up after ourselves, close the file pointerfclose($fp);// Before we start outputting anything, lets prepare our data for printing.// Add things up for a total:$total = 0;foreach ($names as $one => $val) { $total += $val['count']; }// Sort the data by most hitsfunction compare($a, $b) { if ($a['count'] == $b['count']) { return 0; } else return (($a['count'] < $b['count']) ? 1 : -1);}uasort($names, 'compare');// Lets start our web page?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Specific Browser Statistics</title><style>body { font-family: Geneva, Arial, Sans Serif; background-color: #ECECEC; text-align: center; font-size: 11px;}table { border: 2px solid black; }th, td { border: 1px solid black; padding: 1px; margin: 1px;}td { text-align: right; }.sm { font-size: 10px; }.name { text-align: left; }table.sub { background-color: #E0E0E0; margin-left: 10px;}</style></head><body><h1>Specific Browser Statistics</h1><table><tr><th scope="col" width="1%" style="background-color: #999999">No.</th><th scope="col" colspan="2" style="background-color: #CCCC00">Hits</th><th scope="col" style="background-color: #9999FF">Browser</th><th scope="col" style="background-color: #CC00CC">Cuml</th></tr><tr><td colspan="5"></td></tr><?php// Loop through all specifics ...$count = 1;$cuml = 0;foreach ($names as $one => $val) { // The Number echo '<tr><th scope="row">', $count++, "</th>\n"; // The Count echo "<td>{$val['count']}</td>\n"; $cuml += $val['count']; // The Percent printf("<td> %.2f%%</td>\n", 100.00 * ($val['count'] / $total)); // The Name echo "<td class=\"name\"> {$one}"; // If it has subs ... if (isset($val['subs'])) { // Print them echo "<table class=\"sub sm\">\n"; // Sort them ... arsort($val['subs']); foreach ($val['subs'] as $sb => $sbnum) { // The hits echo "<tr><td> {$sbnum}</td>\n"; // The % printf("<td class=\"sm2\"> %.2f%%</td>\n", 100.00 * ($sbnum / $val['count'])); // The Name echo "<td class=\"name\"> {$sb}</td>\n"; echo "</tr>\n"; } echo "</table>"; } // Finish the Name echo "</td>\n"; // The cuml % printf("<td> %.2f%%</td>\n", 100.00 * ($cuml / $total)); print "</tr>\n";}// End everything up ...?></table></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?