在一个日志文件中,我想知道用户登录失败的次数和日期详细信息我有这个代码,但它无法正常工作。
这是一个代码
$file = new SplFileObject("failed.log", "r");
while(!$file->eof())
{
$data[] = current (($file->fgetcsv("|"))) . next (($file->fgetcsv("|")));
}
$abc = implode(",", $data);
echo $abc;
$br = "\n";
在这段代码中我得到了这样的
John26:Dec:2017:16:15:27,Abc29:Dec:2017:09:39:45,Bhumi29/Dec/2017 14:15:41,Abc
它不正确,它读取第一行名称和第二行日期我不会在我的代码中出错。
这是我的日志失败详情
John|26:Dec:2017:16:09:45|192.168.0.101|CentaurOne|Failed
cena|26:Dec:2017:16:15:27|192.168.0.101|CentaurOne|Failed
Abc|27:Dec:2017:09:25:25|192.168.0.101|CentaurOne|Failed
Bhumi|29:Dec:2017:09:39:45|192.168.0.101|CentaurOne|Failed
Bhumi|29:Dec:2017:10:19:28|192.168.0.101|CentaurOne|Failed
Avccc|29/Dec/2017 14:15:41|192.168.0.101|CentaurOne|Failed
Abc|04/Jan/2018 15:54:35|192.168.0.101|CentaurOne|Failed
答案 0 :(得分:1)
正如评论中所建议的那样,您应该重新考虑将失败的登录保存到文本日志,特别是如果您打算将其作为例行操作。随着文件的增长,它将变得占用大量内存:
# Saves contents of file into array by end of lines
$explode = file("failed.log",FILE_SKIP_EMPTY_LINES);
# Loop rows
foreach($explode as $row) {
# Explode the | character
$exp = explode('|',$row);
# Save the user to the an array
$new[$exp[0]]['dates'][] = $exp[1];
# Count how many times tried
if(!isset($new[$exp[0]]['count']))
$new[$exp[0]]['count'] = 1;
else
$new[$exp[0]]['count'] += 1;
}
print_r($new);
应该给你:
Array
(
[John] => Array
(
[dates] => Array
(
[0] => 26:Dec:2017:16:09:45
)
[count] => 1
)
[cena] => Array
(
[dates] => Array
(
[0] => 26:Dec:2017:16:15:27
)
[count] => 1
)
[Abc] => Array
(
[dates] => Array
(
[0] => 27:Dec:2017:09:25:25
[1] => 04/Jan/2018 15:54:35
)
[count] => 2
)
[Bhumi] => Array
(
[dates] => Array
(
[0] => 29:Dec:2017:09:39:45
[1] => 29:Dec:2017:10:19:28
)
[count] => 2
)
[Avccc] => Array
(
[dates] => Array
(
[0] => 29/Dec/2017 14:15:41
)
[count] => 1
)
)
使用print_r()
查看如何访问数组键/值。