Skip to content

Instantly share code, notes, and snippets.

@lnrsoft
Created May 26, 2019 17:32
Show Gist options
  • Save lnrsoft/7371f603ddf6ebf81639fcad9dba553b to your computer and use it in GitHub Desktop.
Save lnrsoft/7371f603ddf6ebf81639fcad9dba553b to your computer and use it in GitHub Desktop.
Here is a quick solution written in php that you can use to determine the current IP address of a given webhost/domain.
<?php
echo GetClientIP(true);
function GetClientIP($validate = False)
{
$ipkeys = array(
'REMOTE_ADDR',
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP'
);
$ip = array();
foreach ($ipkeys as $keyword)
{
if (isset($_SERVER[$keyword]))
{
if ($validate)
{
if (ValidatePublicIP($_SERVER[$keyword]))
{
$ip[] = $_SERVER[$keyword];
}
}
else
{
$ip[] = $_SERVER[$keyword];
}
}
}
$ip = (empty($ip) ? 'Unknown' : implode(", ", $ip));
return $ip;
}
function ValidatePublicIP($ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))
{
return true;
}
else
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment