Jump to content
MakeWebGames

Recommended Posts

Posted

Remote IP Detection

use this instead, it will detect the remote IP, even if the player is behind a proxy

 

function validip($ip) {
   if (!empty($ip) && ip2long($ip)!=-1) {
       $reserved_ips = array (
       array('0.0.0.0','2.255.255.255'),
       array('10.0.0.0','10.255.255.255'),
       array('127.0.0.0','127.255.255.255'),
       array('169.254.0.0','169.254.255.255'),
       array('172.16.0.0','172.31.255.255'),
       array('192.0.2.0','192.0.2.255'),
       array('192.168.0.0','192.168.255.255'),
       array('255.255.255.0','255.255.255.255')
       );

       foreach ($reserved_ips as $r) {
           $min = ip2long($r[0]);
           $max = ip2long($r[1]);
           if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
       }
       return true;
   } else {
       return false;
   }
}

function getip() {
   if (validip($_SERVER["HTTP_CLIENT_IP"])) {
       return $_SERVER["HTTP_CLIENT_IP"];
   }
   foreach (explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) {
       if (validip(trim($ip))) {
           return $ip;
       }
   }
   if (validip($_SERVER["HTTP_X_FORWARDED"])) {
       return $_SERVER["HTTP_X_FORWARDED"];
   } elseif (validip($_SERVER["HTTP_FORWARDED_FOR"])) {
       return $_SERVER["HTTP_FORWARDED_FOR"];
   } elseif (validip($_SERVER["HTTP_FORWARDED"])) {
       return $_SERVER["HTTP_FORWARDED"];
   } elseif (validip($_SERVER["HTTP_X_FORWARDED"])) {
       return $_SERVER["HTTP_X_FORWARDED"];
   } else {
       return $_SERVER["REMOTE_ADDR"];
   }
}
?>
Guest Anonymous
Posted

Re: Strange Ip problem

echo the getip() function.

 

echo getip();
Posted

Re: Strange Ip problem

 

lol

Thanks....I didn't echo it, lol...thats why..

Once strange thing...

It makes everyones IP mine............

i used

echo getip();

any ideas....

Where are you placing that bit?

that is a pretty good replacement to $IP = ....

$IP = getip();

Posted

Re: Strange Ip problem

i added it to viewuser.php.

No no no, Your supposed to have this for the updating part (lastip) (Think its in header) the viewuser part should stay the same

Posted

Re: Strange Ip problem

I have used MD's idea and i have error_reporting(E_ALL); on my WAMP server i get a bunch Undefined index does anyone know how to fix this and leave error_reporting(E_ALL); on?

 

Notice: Undefined index: HTTP_CLIENT_IP in C:\wamp\www\Cursed-Islands (Re-Make)\global_func.php on line 417

Notice: Undefined index: HTTP_X_FORWARDED_FOR in C:\wamp\www\Cursed-Islands (Re-Make)\global_func.php on line 421

Notice: Undefined index: HTTP_X_FORWARDED in C:\wamp\www\Cursed-Islands (Re-Make)\global_func.php on line 428

Notice: Undefined index: HTTP_FORWARDED_FOR in C:\wamp\www\Cursed-Islands (Re-Make)\global_func.php on line 432

Notice: Undefined index: HTTP_FORWARDED in C:\wamp\www\Cursed-Islands (Re-Make)\global_func.php on line 436

Notice: Undefined index: HTTP_X_FORWARDED in C:\wamp\www\Cursed-Islands (Re-Make)\global_func.php on line 440
Posted

Re: Strange Ip problem

Ahh good point. I've used it isset() before but i haven't fully got grasp on when and where to use it.

EDIT: If you have error_reporting(E_ALL); on use this:

 

function validip($IP)
{
if (!empty($IP) && ip2long($IP)!=-1)
{
	$ReservedIps = array (
		array('0.0.0.0','2.255.255.255'), 
		array('10.0.0.0','10.255.255.255'), 
		array('127.0.0.0','127.255.255.255'), 
		array('169.254.0.0','169.254.255.255'), 
		array('172.16.0.0','172.31.255.255'), 
		array('192.0.2.0','192.0.2.255'), 
		array('192.168.0.0','192.168.255.255'),
		array('255.255.255.0','255.255.255.255')
       );

	foreach ($ReservedIps as $R)
	{
		$Min = ip2long($R[0]);
		$Max = ip2long($R[1]);
		if ((ip2long($IP) >= $Min) && (ip2long($IP) <= $Max)) return false;
	}
	return true;
}
else
{
       return false;
   }
}

function getip()
{
if (validip(isset($_SERVER['HTTP_CLIENT_IP'])))
{
	return $_SERVER['HTTP_CLIENT_IP'];
   }
foreach (explode(",", isset($_SERVER['HTTP_X_FORWARDED_FOR'])) as $IP)
{
	if (validip(trim($IP)))
	{
           return $IP;
       }
   }
if (validip(isset($_SERVER['HTTP_X_FORWARDED'])))
{
	return $_SERVER['HTTP_X_FORWARDED'];
   }
else if (validip(isset($_SERVER['HTTP_FORWARDED_FOR'])))
{
	return $_SERVER['HTTP_FORWARDED_FOR'];
   }
else if (validip(isset($_SERVER['HTTP_FORWARDED'])))
{
	return $_SERVER['HTTP_FORWARDED'];
   }
else if (validip(isset($_SERVER['HTTP_X_FORWARDED'])))
{
	return $_SERVER['HTTP_X_FORWARDED'];
   }
else
{
	return $_SERVER['REMOTE_ADDR'];
}
}
Guest Anonymous
Posted

Re: Strange Ip problem

mm.. almost right... except

a) it appears to handle an invalid list of reserved for internal use IPs

b) it only handles IPv4 addresses

Posted

Re: Strange Ip problem

Umm Hi ,

I got told this will show the person behind the proxy?

i tried this formular and it returned

IP Hostname

Last Hit 67.159.56.82 fsck.y-0-u.org

Last Login 67.159.56.82 fsck.y-0-u.org

That is no where near my real ip....

or ISP ip.....

Is there some sort of trick to this that you know and i don't?

Posted

Re: Strange Ip problem

Umm Hi ,

I got told this will show the person behind the proxy?

i tried this formular and it returned

IP Hostname

Last Hit 67.159.56.82 fsck.y-0-u.org

Last Login 67.159.56.82 fsck.y-0-u.org

That is no where near my real ip....

or ISP ip.....

Is there some sort of trick to this that you know and i don't?

By anychance are you using a proxy?

  • 3 weeks later...
Posted

Re: Strange Ip problem

I've been looking around and simple why not just use

function IP($IP)
{
return long2ip(ip2long($IP));
}

 

Example #3 IP validation
ip2long() should not be used as the sole form of IP validation. Combine it with long2ip():

<?php
// make sure IPs are valid. also converts a non-complete IP into
// a proper dotted quad as explained below.
$ip = long2ip(ip2long("127.0.0.1")); // "127.0.0.1"
$ip = long2ip(ip2long("10.0.0")); // "10.0.0.0"
$ip = long2ip(ip2long("10.0.256")); // "10.0.1.0"
?>

 

Handles IPv4 Addresses Only.

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...