function detect real ip (updated)

Started on the old forum in April 2011 · view the original in the archive · old links redirect here

17 replies · 4,719 views · started 15 years ago · 1 min read

TL;DR · summary of 17 replies
  • The function was updated and now checks for a valid IP using filter_var with FILTER_VALIDATE_IP and FILTER_FLAG_NO_PRIV_RANGE.
  • It also includes a trim() function in the code to clean up any whitespace or other characters that might affect the IP validation.
  • The function is intended to work on Nginx, Opera Mini devices, and proxies, returning 0 for the last octet of an IP address.

Written automatically from the replies by a language model on our own server. It can be wrong; the posts below are the source.

7 participants
GumSlone Administrator Principal · 1,690 posts #1
function gum_real_ip()
{
     $keyname_ip_arr = array('HTTP_X_FORWARDED_FOR', 'HTTP_REMOTE_ADDR_REAL', 'HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'REMOTE_ADDR');
    foreach ($keyname_ip_arr as $keyname_ip) {
  if (!empty($_SERVER[$keyname_ip])) {
   $ip = $_SERVER[$keyname_ip];
   if (strstr($ip, ',')) {
    $ips = explode(',', $ip);
    foreach ($ips as $ip)
    {
      if(filter_var(trim($ip), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) break(2);
    }
   }
   elseif(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) break;
  }
    }
    return $ip;
    list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip);
    return $ip1.'.'.$ip2.'.'.$ip3.'.0'; //REAL IP
}

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Anshul Senior · 296 posts #2

GumSlone wrote:

function gum_real_ip()
{

	$keyname_ip_arr = array('HTTP_X_FORWARDED_FOR', 'HTTP_REMOTE_ADDR_REAL', 'HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'REMOTE_ADDR');
	foreach ($keyname_ip_arr as $keyname_ip) {
		if (!empty($_SERVER[$keyname_ip])) {
			$ip = urlencode($_SERVER[$keyname_ip]);
			break;
		}
	}
	if (strstr($ip, ',')) {
		$ips = explode(',', $ip);
		$ip = $ips[0];
	}
	if(empty($ip)) $ip = $_SERVER["REMOTE_ADDR"];
	
	list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip);
	return $ip1.'.'.$ip2.'.'.$ip3.'.0'; //REAL IP
}

is this will work on ngix wid mini opera nd proxies ?? and last subnet 0 ?? isnt it wil return 0 in the last of all ip's ??

GumSlone Administrator Principal · 1,690 posts #3

Anshul wrote: is this will work on ngix wid mini opera nd proxies ?? and last subnet 0 ?? isnt it wil return 0 in the last of all ip's ??

yes it will return the last 0 for all ips, and it should work with nginx and opera and other proxies, this it the latest function which i am using on my sites, well, if you dont want to have the last number in ip =0; you can simply replace :

    list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip); 
    return $ip1.'.'.$ip2.'.'.$ip3.'.0'; //REAL IP

by

return $ip;

also if you dont use apache's mod_geo_ip for ip to coutry detection its better to set last ip value to zero and cache the result of geo ip detection for this ip eg. with memcached, this will reduce the server load.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Leviathan73 Senior · 260 posts #4

gumslone.. but they put in the core, or in the user profile?😕

Anshul Senior · 296 posts #7

GumSlone wrote: yes it will return the last 0 for all ips, and it should work with nginx and opera and other proxies, this it the latest function which i am using on my sites, well, if you dont want to have the last number in ip =0; you can simply replace :

    list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip); 
    return $ip1.'.'.$ip2.'.'.$ip3.'.0'; //REAL IP

by

return $ip;

also if you dont use apache's mod_geo_ip for ip to coutry detection its better to set last ip value to zero and cache the result of geo ip detection for this ip eg. with memcached, this will reduce the server load.

ok cool i dont use seperate queries for updatin ip anyway its wid last active time anyway i wil giv a try to this 🙂

something else Fellow · 2,296 posts #9

is relying on HTTP_X_FORWARDED_FOR as your first choice to get real ip a good idea ? As it is probably the most common way of spoofing ip number

GumSlone Administrator Principal · 1,690 posts #10

something else wrote: is relying on HTTP_X_FORWARDED_FOR as your first choice to get real ip a good idea ? As it is probably the most common way of spoofing ip number

well, the HTTP_X_FORWARDED_FOR will work wth all opera mini users.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

something else Fellow · 2,296 posts #11

Yeah i was going to say you could use something like:

if(!isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']))
         //re-arange array

however this will also miss poor proxys also so maybe also an else statement something like:

else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])&&$_SERVER['HTTP_X_FORWARDED_FOR']!=$_SERVER['REMOTE_ADDR'])
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

also finally to prevent hacking:

return (int)$ip1.'.'.(int)$ip2.'.'.(int)$ip3.'.0'; //REAL IP
GumSlone Administrator Principal · 1,690 posts #12

something else wrote: Yeah i was going to say you could use something like:

if(!isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']))
         //re-arange array

however this will also miss poor proxys also so maybe also an else statement something like:

else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])&&$_SERVER['HTTP_X_FORWARDED_FOR']!=$_SERVER['REMOTE_ADDR'])
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

also finally to prevent hacking:

return (int)$ip1.'.'.(int)$ip2.'.'.(int)$ip3.'.0'; //REAL IP

if someone wants to change/hide ip he can do it easily and there is no way to detect a real ip in such cases, this function is made to detect ip of other 99% normal site users who use opera or use other different proxys to save their traffic costs and not trying to hide their location/ip, you could also add a function which checks if the detected ip is valid and if not then skip back to remote_addr.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

arnage Lead · 811 posts #13

I discard everything invalid, even with commas.

function user_ip() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED'])) {
$ip = $_SERVER['HTTP_X_FORWARDED'];
}
elseif (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_FORWARDED_FOR'];
}
elseif (!empty($_SERVER['HTTP_FORWARDED'])) {
$ip = $_SERVER['HTTP_FORWARDED'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
if (isset($ip) && !preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/', $ip)) {
$ip = 'ProxyIP';
}
return $ip;
}
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
GumSlone Administrator Principal · 1,690 posts #14

arnage wrote: I discard everything invalid, even with commas.

function user_ip() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED'])) {
$ip = $_SERVER['HTTP_X_FORWARDED'];
}
elseif (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_FORWARDED_FOR'];
}
elseif (!empty($_SERVER['HTTP_FORWARDED'])) {
$ip = $_SERVER['HTTP_FORWARDED'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
if (isset($ip) && !preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/', $ip)) {
$ip = 'ProxyIP';
}
return $ip;
}

the ip range with comma contains the real ip of opera mini user, i use my function for ip to country detection, because its more correct than the default mod geoip which detects country by remote_addr.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

CuteAmit Junior · 35 posts #15

hello guys,

why we using HTTP_X_FORWARDED_FOR', 'HTTP_REMOTE_ADDR_REAL', 'HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'REMOTE_ADDR, to detect ip ???

all these are similar to REMOTE_ADDR ??

For Latest Technology Tips & Tricks Click Here

GumSlone Administrator Principal · 1,690 posts #16

function updated added filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) which check if the ip is private

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

kiss Senior · 294 posts #18
 function ip( $default = null, $filter_options = 12582912 )
 {
  $HTTP_X_FORWARDED_FOR = isset( $_SERVER ) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : getenv( 'HTTP_X_FORWARDED_FOR' ) ;
  $HTTP_X_REAL_IP = isset( $_SERVER ) ? $_SERVER["HTTP_X_REAL_IP"] : getenv( 'HTTP_X_REAL_IP' ) ;
  $HTTP_CLIENT_IP = isset( $_SERVER ) ? $_SERVER["HTTP_CLIENT_IP"] : getenv( 'HTTP_CLIENT_IP' ) ;
  $HTTP_X_CLUSTER_CLIENT_IP = isset( $_SERVER ) ? $_SERVER["HTTP_X_CLUSTER_CLIENT_IP"] : getenv( 'HTTP_X_CLUSTER_CLIENT_IP' ) ;
  $HTTP_X_SUCURI_CLIENTIP = isset( $_SERVER ) ? $_SERVER["HTTP_X_SUCURI_CLIENTIP"] : getenv( 'HTTP_X_SUCURI_CLIENTIP' ) ;
  $HTTP_CF_CONNECTING_IP = isset( $_SERVER ) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : getenv( 'HTTP_CF_CONNECTING_IP' ) ;
  $REMOTE_ADDR = isset( $_SERVER ) ? $_SERVER["REMOTE_ADDR"] : getenv( 'REMOTE_ADDR' ) ;
  $all_ips = explode( ",", "$HTTP_X_FORWARDED_FOR,$HTTP_X_REAL_IP,$HTTP_CLIENT_IP,$HTTP_X_CLUSTER_CLIENT_IP,$HTTP_X_SUCURI_CLIENTIP,$HTTP_CF_CONNECTING_IP,$REMOTE_ADDR" ) ;
  foreach ( $all_ips as $ip ) {
   if ( $ip = filter_var( $ip, FILTER_VALIDATE_IP, $filter_options ) )
    break ;
  }
  return $ip ? $ip : $default ;
 }

www.inbuzunar.mobi -Your mobile portal pocket

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close