场景:有一台服务器是IIS架构,想了解其中一个js(index.js)每天被其他网站调用的情况。 首先下载所有的IIS日志架构一个TP6.0程序后放入 runtime/log 文件夹中。
public function checkIndexJs()
{
$file = root_path() . DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR . 'log';
$temp = scandir($file);
// 遍历文件夹
$result = [];
$resultAll = [];
foreach ($temp as $v) {
$log = $file . DIRECTORY_SEPARATOR . $v;
if (file_exists($log) && $v !== '.' && $v !== '..') {
// 读取文件内容
$info = fopen($log, "r");
// 输出文本中所有的行,直到文件结束为止。
while (!feof($info)) {
// fgets()函数从文件指针中读取一行
$itemStr = fgets($info);
// 判断是否包含index.js
if (strpos($itemStr, 'index.js') !== false) {
preg_match("/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/i", $itemStr, $domain);
if (isset($domain[2])) {
// 放入数组,方便计算,去除重复
$a = $result[$v] ?? [];
// 记录到当前数组
if (!in_array($domain[2], $a)) {
$result[$v][] = $domain[2];
}
if (!in_array($domain[2], $resultAll)) {
$resultAll[] = $domain[2];
}
}
}
}
fclose($info);
}
}
dump($result, $resultAll);
}
实现目标及原理:循环log目录的每一个文件,读取每一行,判断是否包含index.js,然后读取来源域名并存入不重复的数组内,这样我们得到的结果就是所有访问过(调用过)index.js的域名和每天的一个调用情况。
来源:oschina
链接:https://my.oschina.net/siyucms/blog/3193900