-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_users.php
80 lines (67 loc) · 1.96 KB
/
log_users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
function writeVisitorToLog() {
try {
if (!isDocker()) {
$logFilePath = '/var/log/asanai_visitors.log';
if(!file_exists($logFilePath)) {
return;
}
$userId = getUserId();
$logLines = array_map('trim', file($logFilePath));
$updatedLogLines = [];
$userFound = false;
foreach ($logLines as $line) {
list($storedUserId, $visits) = explode(':', $line);
if ($storedUserId === $userId) {
$visits = intval($visits) + 1;
$line = "$userId:$visits";
$userFound = true;
}
$updatedLogLines[] = $line;
}
if (!$userFound) {
$updatedLogLines[] = "$userId:1";
}
file_put_contents($logFilePath, implode("\n", $updatedLogLines));
}
} catch (Exception $e) {
//
}
}
function getUserId() {
return md5($_SERVER['HTTP_USER_AGENT'] ?? "" . (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "") . rand()); // no await, not js
}
function isDocker() {
$contents = @file_get_contents('/proc/1/cgroup');
return (strpos($contents, '/docker/') !== false);
}
function logReferrer() {
try {
$referrerLogFilePath = '/var/log/asanai_referrers.log';
if (!file_exists($referrerLogFilePath)) {
file_put_contents($referrerLogFilePath, ''); // Create the file if it doesn't exist
}
$referrer = $_SERVER['HTTP_REFERER'] ?? "No Referrer"; // Get the HTTP referrer or set a default value
$referrerLines = array_map('trim', file($referrerLogFilePath));
// Check if the referrer is already logged
$referrerFound = false;
foreach ($referrerLines as $line) {
if ($line === $referrer) {
$referrerFound = true;
break;
}
}
if (!$referrerFound) {
file_put_contents($referrerLogFilePath, $referrer . "\n", FILE_APPEND); // Log the referrer if not found
}
} catch (\Throwable $e) {
// ignore
}
}
try {
writeVisitorToLog();
@logReferrer(); // Log the referrer without requiring user ID
} catch (Exception $e) {
// Ignore exception
}
?>