Adds user IP to the logs.

main
Yasen Pramatarov 2024-09-17 14:22:43 +03:00
parent 527da25cdc
commit be0cd48c01
3 changed files with 27 additions and 3 deletions

View File

@ -0,0 +1,22 @@
<?php
function getUserIP() {
// get directly the user IP
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
// if user is behind some proxy
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
// get only the first IP if there are more
if (strpos($ip, ',') !== false) {
$ip = explode(',', $ip)[0];
}
return trim($ip);
}
?>

View File

@ -41,7 +41,7 @@ try {
// redirect to index // redirect to index
$_SESSION['notice'] = "Login successful"; $_SESSION['notice'] = "Login successful";
$user_id = $userObject->getUserId($username)[0]['id']; $user_id = $userObject->getUserId($username)[0]['id'];
$logObject->insertLog($user_id, "Login: User \"$username\" logged in.", 'user'); $logObject->insertLog($user_id, "Login: User \"$username\" logged in. IP: $user_IP", 'user');
header('Location: index.php'); header('Location: index.php');
exit(); exit();
@ -49,7 +49,7 @@ try {
} else { } else {
$_SESSION['error'] = "Login failed."; $_SESSION['error'] = "Login failed.";
$user_id = $userObject->getUserId($username)[0]['id']; $user_id = $userObject->getUserId($username)[0]['id'];
$logObject->insertLog($user_id, "Login: Failed login attempt for user \"$username\".", 'user'); $logObject->insertLog($user_id, "Login: Failed login attempt for user \"$username\". IP: $user_IP", 'user');
header('Location: index.php'); header('Location: index.php');
exit(); exit();
} }

View File

@ -111,7 +111,9 @@ $dbWeb = connectDB($config);
// start logging // start logging
require '../app/classes/log.php'; require '../app/classes/log.php';
include '../app/helpers/logs.php';
$logObject = new Log($dbWeb); $logObject = new Log($dbWeb);
$user_IP = getUserIP();
// get platforms details // get platforms details
require '../app/classes/platform.php'; require '../app/classes/platform.php';
@ -133,7 +135,7 @@ if ($page == 'logout') {
$notice = "You were logged out.<br />You can log in again."; $notice = "You were logged out.<br />You can log in again.";
$user_id = $userObject->getUserId($currentUser)[0]['id']; $user_id = $userObject->getUserId($currentUser)[0]['id'];
$logObject->insertLog($user_id, "Logout: User \"$currentUser\" logged out.", 'user'); $logObject->insertLog($user_id, "Logout: User \"$currentUser\" logged out. IP: $user_IP", 'user');
// clean up session // clean up session
session_unset(); session_unset();