Korjaa getClientIp() X-Forwarded-For parsinta + näytä IP virheessä
X-Forwarded-For voi sisältää useita IP:tä pilkulla erotettuna. Otetaan nyt vain ensimmäinen (asiakkaan oikea IP). Lisäksi näytetään havaittu IP virheviestissä debuggausta varten. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
11
api.php
11
api.php
@@ -127,7 +127,14 @@ function companyFile(string $filename): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getClientIp(): string {
|
function getClientIp(): string {
|
||||||
return $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
|
$xff = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? '';
|
||||||
|
if ($xff) {
|
||||||
|
// X-Forwarded-For voi sisältää useita IP:itä: "client, proxy1, proxy2" — otetaan ensimmäinen
|
||||||
|
$parts = explode(',', $xff);
|
||||||
|
$ip = trim($parts[0]);
|
||||||
|
if (filter_var($ip, FILTER_VALIDATE_IP)) return $ip;
|
||||||
|
}
|
||||||
|
return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1163,7 +1170,7 @@ switch ($action) {
|
|||||||
if (empty($allowedCompanies)) {
|
if (empty($allowedCompanies)) {
|
||||||
dbRecordLoginAttempt($ip);
|
dbRecordLoginAttempt($ip);
|
||||||
http_response_code(403);
|
http_response_code(403);
|
||||||
echo json_encode(['error' => 'IP-osoitteesi ei ole sallittu.']);
|
echo json_encode(['error' => 'IP-osoitteesi (' . $ip . ') ei ole sallittu.']);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$userCompanies = $allowedCompanies;
|
$userCompanies = $allowedCompanies;
|
||||||
|
|||||||
Reference in New Issue
Block a user