From 6aa41133109e5b94286f06cbf22968c3bb8c5f05 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 27 Feb 2023 23:44:55 +0100 Subject: [PATCH] Log: fix logging the right IP address when install behind a reverse proxy --- src/Log.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Log.php b/src/Log.php index c2caf9a..65bbe83 100644 --- a/src/Log.php +++ b/src/Log.php @@ -133,6 +133,20 @@ class Log { set_exception_handler(array('EesyPHP\\Log', 'exception')); } + /** + * Get remote IP address + * @return string + */ + public static function get_remote_addr(){ + if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) + return $_SERVER["HTTP_X_FORWARDED_FOR"]; + if (array_key_exists('REMOTE_ADDR', $_SERVER)) + return $_SERVER["REMOTE_ADDR"]; + if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) + return $_SERVER["HTTP_CLIENT_IP"]; + return ''; + } + /** * Log a message * @param string $level The message level (key of self :: $levels) @@ -171,7 +185,7 @@ class Log { $msg = array( date('Y/m/d H:i:s'), $_SERVER['REQUEST_URI'], - $_SERVER['REMOTE_ADDR'], + self :: get_remote_addr(), ); if (Auth::enabled()) $msg[] = (Auth::user()?Auth::user():'anonymous');