Skip to content

Instantly share code, notes, and snippets.

@raksa
Created September 25, 2018 09:36
Show Gist options
  • Save raksa/66b96969cb27af11a3d1466d7105c81f to your computer and use it in GitHub Desktop.
Save raksa/66b96969cb27af11a3d1466d7105c81f to your computer and use it in GitHub Desktop.
get client ip address
<?php
function getPublic()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ipinfo.io/ip",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Postman-Token: 83e4eed8-5cd2-4ff5-8716-5f4849d12911",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
function staticGetIP()
{
$ip = '127.0.0.1';
if (getenv('HTTP_CF_CONNECTING_IP')) {
$ip = getenv('HTTP_CF_CONNECTING_IP');
} else if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} else if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} else if (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
} else if (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
} else if (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
} else if (getenv('REMOTE_ADDR')) {
$ip = getenv('REMOTE_ADDR');
} else {
$ip = getPublic();
}
return explode(',', $ip)[0];
}
echo staticGetIP();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment