0% found this document useful (0 votes)
181 views

Getip PHP

This PHP function gets the client's IP address by checking several server variables like HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR for a valid IP address. It first checks for these variables in the $_SERVER array, then explodes any comma separated values and trims whitespace before validating the IP address is not private or reserved using a filter. The first valid public IP address found is returned.

Uploaded by

tiberiubabet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views

Getip PHP

This PHP function gets the client's IP address by checking several server variables like HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR for a valid IP address. It first checks for these variables in the $_SERVER array, then explodes any comma separated values and trims whitespace before validating the IP address is not private or reserved using a filter. The first valid public IP address found is returned.

Uploaded by

tiberiubabet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

<?

php
function getIP(){//obtine adresa IP client
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADD
R') as $key){
if (array_key_exists($key, $_SERVER) === true){
foreach (explode(',', $_SERVER[$key]) as $ip){
$ip = trim($ip); // just to be safe
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANG
E | FILTER_FLAG_NO_RES_RANGE) !== false){
return $ip;
}}}}}
?>

You might also like