2、操作系統信息

3、瀏覽器

用腳本統計最近一個月的訪問日志,得到以下結果:
- 搜索引擎爬蟲比較頻繁,每天有好幾千次數據訪問;
- Windows仍是份額最大的操作系統,Linux桌面依然份額很小;
- Chrome目前是瀏覽器領域的霸主,其次是Firefox,Opera已經很小眾了。
最后附上PHP腳本的代碼,也可以從本人的Github里找到:https://github.com/tlanyan/Scripts/blob/master/statUA.php
#!/usr/bin/php
<?php
/**
* @brief stat UA in access log
*
* @author tlanyan<tlanyan@hotmail.com>
* @link http://tlanyan.me
*/
/* vim: set ts=4; set sw=4; set ss=4; set expandtab; */
function getFileList(string $path) : array {
return glob(rtrim($path, "/") . "/*access.log*");
}
function statFiles(array $files) : array {
$stat = [];
echo PHP_EOL, "start to read files...", PHP_EOL;
foreach ($files as $file) {
echo "read file: $file ...", PHP_EOL;
$contents = getFileContent($file);
foreach ($contents as $line) {
$ua = getUA($line);
if (isset($stat[$ua])) {
$stat[$ua] += 1;
} else {
$stat[$ua] = 1;
}
}
}
echo "stat all files done!", PHP_EOL, PHP_EOL;
return $stat;
}
function getFileContent(string $file) : array {
if (substr($file, -3, 3) === ".gz") {
return gzfile($file);
}
return file($file);
}
function getUA(string $line) : ?string {
// important! Nginx log format determins the UA location in the line!
// You may have to refactor following codes to get the right result
// UA starts from fifth double quote
$count = 0; $offset = 0;
while ($count < 5) {
$pos = strpos($line, '"', $offset);
if ($pos === false) {
echo "Error! Unknown line: $line", PHP_EOL;
return null;
}
$count ++;
$offset = $pos + 1;
}
$end = strpos($line, '"', $offset);
return substr($line, $offset, $end - $offset);
}
function usage() {
echo "Usage: php statUA.php [option] [dir]", PHP_EOL;
echo " options:", PHP_EOL;
echo " -h: show this help", PHP_EOL;
echo " -v: verbose mode", PHP_EOL;
echo "-n NUM: UA list number", PHP_EOL;
echo " dir: directory to the log files", PHP_EOL;
echo PHP_EOL;
}
function filterUA(array& $stat, array $UAFilters) {
$filterCount = 0;
foreach ($UAFilters as $filter) {
foreach ($stat as $ua => $count) {
if (stripos($ua, $filter) !== false) {
$filterCount += $count;
unset($stat[$ua]);
}
}
}
echo "filter $filterCount records!", PHP_EOL;
}
function printCount(array $stat) {
$sum = array_sum($stat);
foreach ($stat as $key => $count) {
echo $key, " : ", $count, ", percent: ", sprintf("%.2f", 100*$count/$sum), PHP_EOL;
}
}
function statOS(array $UAs) : array {
global $debug;
echo PHP_EOL, "stat OS...", PHP_EOL;
$os = ["Windows", "MacOS", "Linux", "Android", "iOS", "other"];
$stat = array_fill_keys($os, 0);
foreach ($UAs as $key => $count) {
if (strpos($key, "Windows") !== false) {
$stat["Windows"] += $count;
} else if (strpos($key, "Macintosh") !== false) {
$stat["MacOS"] += $count;
// must deal Android first, then Linux
} else if (strpos($key, "Android") !== false) {
$stat["Android"] += $count;
} else if (strpos($key, "Linux") !== false) {
$stat["Linux"] += $count;
} else if (strpos($key, "iPhone") !== false || strpos($key, "iOS") !== false || strpos($key, "like Mac OS") !== false || strpos($key, "Darwin") !== false) {
$stat["iOS"] += $count;
} else {
if ($debug) {
echo "other: $key, count: $count", PHP_EOL;
}
$stat["other"] += $count;
}
}
return $stat;
}
function statBrowser(array $UAs) : array {
global $debug;
echo PHP_EOL, "stat brwoser...", PHP_EOL;
$browsers = ["Chrome", "Firefox", "IE", "Safari", "Edge", "Opera", "other"];
$stat = array_fill_keys($browsers, 0);
foreach ($UAs as $key => $count) {
if (strpos($key, "MSIE") !== false) {
$stat["IE"] += $count;
} else if (strpos($key, "Edge") !== false) {
$stat["Edge"] += $count;
} else if (strpos($key, "Firefox") !== false) {
$stat["Firefox"] += $count;
} else if (strpos($key, "OPR") !== false) {
$stat["Opera"] += $count;
// first Chrome, then Safari
} else if (strpos($key, "Chrome") !== false) {
$stat["Chrome"] += $count;
} else if (strpos($key, "Safari") !== false) {
$stat["Safari"] += $count;
} else {
if ($debug) {
echo "other: $key, count: $count", PHP_EOL;
}
$stat["other"] += $count;
}
}
return $stat;
}
function parseCmd() {
global $debug, $num, $path, $argc, $argv;
$optind = null;
$options = getopt("hvn:", [], $optind);
if ($argc > 2 && empty($options)) {
usage();
exit(1);
}
if (isset($options['h'])) {
usage();
exit(0);
}
if (isset($options['v'])) {
$debug = true;
}
if (isset($options['n'])) {
$num = intval($options['n']);
if ($num <= 0) {
$num = 10;
}
}
if ($argc === 2 && empty($options)) {
$path = $argv[1];
}
if ($argc > $optind) {
$path = $argv[$optind];
}
if (!is_dir($path)) {
echo "invalid directory: $path", PHP_EOL;
exit(1);
}
if ($debug) {
echo "num: $num", PHP_EOL;
echo "verbose: ", var_export($debug, true), PHP_EOL;
echo "path: $path", PHP_EOL;
}
}
if (version_compare(PHP_VERSION, "7.1") < 0) {
exit("scripts require PHP >=7.1");
}
$path = ".";
$debug = false;
$num = 10;
$UAFilters = [
"spider",
"bot",
"wget",
"curl",
];
parseCmd();
$files = getFileList($path);
if (empty($files)) {
echo '"' . realpath($path) . '" does not contain access log files.', PHP_EOL;
exit(0);
}
$allUA = statFiles($files);
if (empty($allUA)) {
echo "no data", PHP_EOL;
exit(0);
}
filterUA($allUA, $UAFilters);
// sort array with count
uasort($allUA, function ($a, $b) {
return $b - $a;
});
if ($debug) {
print_r($allUA);
}
echo PHP_EOL, "---- top $num UA ----", PHP_EOL;
printCount(array_slice($allUA, 0, $num));
echo "-------------------", PHP_EOL;
$os = statOS($allUA);
echo PHP_EOL, "os count:", PHP_EOL;
printCount($os);
$browser = statBrowser($allUA);
echo PHP_EOL, "browser count:", PHP_EOL;
printCount($browser);
聲明:本網站發布的內容(圖片、視頻和文字)以原創、轉載和分享網絡內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。郵箱:3140448839@qq.com。本站原創內容未經允許不得轉載,或轉載時需注明出處:
三五互聯知識庫 »
利用PHP如何統計Nginx日志的User Agent數據
主站蜘蛛池模板:
免费国产拍久久受拍久久|
亚洲精品456在线|
福利一区二区视频在线|
日本高清二区视频久二区|
亚洲另类专区中文字幕|
亚洲AV电影在线观看|
四虎成人精品国产永久免费|
久久精品国产99久久久古代|
亚洲电影天堂二区|
久久这里只有精品1|
熟女精品国产一区二区三区|
人妻口爆|
国产成人亚洲人欧洲|
国产蜜芽尤物在线一区|
成人福利网站导航秘 涩涩屋|
人妻聚色窝窝人体WWW一区|
极品少妇的粉嫩小泬看片|
中文字幕第4页|
国产熟睡乱子伦视频在线播放|
亚洲日韩欧美国产高清αv|
99久久99久久免费精品蜜桃|
18禁裸乳无遮挡自慰免费动漫|
国产成人亚洲精品在线看|
国产福利在线观看免费第一福利|
国产成人综合亚洲网|
无码欧美日韩二区三区蜜桃|
草莓视频污在线观看|
久久人人爽av亚洲精品|
亚洲gv天堂无码男同在线观看|
欧美a级欧美1级a大片|
一区二区三区av|
在线观看91香蕉国产免费|
欧美极品色午夜在线视频|
日韩精品亚洲专在线电影|
国产又色又爽又黄的免费软件|
国产拍揄自揄精品视频网站|
成人福利影院|
国产精品亚洲专区无码导航|
国产色视频网免费|
亚洲日韩天堂|
国产精品久久久久久无毒不卡|