Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get user real IP when running BOCA with a reverse proxy #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,20 @@ function getFunctionName($num=2) {
}

function getIP() {
if (getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
if (getenv("REMOTE_ADDR")) {
// Create an array of trusted reverse proxies set via env variable
if (getenv("BOCA_TRUSTED_PROXIES"))
$proxies = explode(",", getenv("BOCA_TRUSTED_PROXIES"));

// Check whether REMOTE_ADDR is actually the IP of a trusted proxy
if ($proxies &&
in_array(getenv("REMOTE_ADDR"), $proxies) &&
getenv("HTTP_X_FORWARDED_FOR"))
// If so, BOCA might be behind a proxy server (e.g., Traefik) in which
// case the proxy may have set the $_SERVER['HTTP_X_FORWARDED_FOR'].
$ip = getenv("HTTP_X_FORWARDED_FOR");
else $ip = getenv("REMOTE_ADDR");
}
else
return "UNKNOWN";
if(defined("dbcompat_1_4_1") && dbcompat_1_4_1==true) return $ip;
Expand Down