« Previous entry | Next entry » Browse > Snippets
Skip to comments (26)
Get visitors IP in PHP
Posted by Niek on Oct 15 2005 @ 20:01 :: 7717 unique visits
Get the ip address of a visitor. Will also try to get the correct IP when the visitor uses a proxy.ip.inc.php:
CODE: PHP
<?
function ip_first($ips) {
if (($pos = strpos($ips, ',')) != false) {
return substr($ips, 0, $pos);
} else {
return $ips;
}
}
function ip_valid($ips) {
if (isset($ips)) {
$ip = ip_first($ips);
$ipnum = ip2long($ip);
if ($ipnum !== -1 && $ipnum !== false && (long2ip($ipnum) === $ip)) { // PHP 4 and PHP 5
if (($ipnum < 167772160 || $ipnum > 184549375) && // Not in 10.0.0.0/8
($ipnum < -1408237568 || $ipnum > -1407188993) && // Not in 172.16.0.0/12
($ipnum < -1062731776 || $ipnum > -1062666241)) // Not in 192.168.0.0/16
return true;
}
}
return false;
}
function ip() {
$check = array(
'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED', 'HTTP_VIA', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM',
'HTTP_CLIENT_IP'
);
foreach ($check as $c) {
if (ip_valid(&$_SERVER[$c])) {
return ip_first($_SERVER[$c]);
}
}
return $_SERVER['REMOTE_ADDR'];
}
?>
function ip_first($ips) {
if (($pos = strpos($ips, ',')) != false) {
return substr($ips, 0, $pos);
} else {
return $ips;
}
}
function ip_valid($ips) {
if (isset($ips)) {
$ip = ip_first($ips);
$ipnum = ip2long($ip);
if ($ipnum !== -1 && $ipnum !== false && (long2ip($ipnum) === $ip)) { // PHP 4 and PHP 5
if (($ipnum < 167772160 || $ipnum > 184549375) && // Not in 10.0.0.0/8
($ipnum < -1408237568 || $ipnum > -1407188993) && // Not in 172.16.0.0/12
($ipnum < -1062731776 || $ipnum > -1062666241)) // Not in 192.168.0.0/16
return true;
}
}
return false;
}
function ip() {
$check = array(
'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED', 'HTTP_VIA', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM',
'HTTP_CLIENT_IP'
);
foreach ($check as $c) {
if (ip_valid(&$_SERVER[$c])) {
return ip_first($_SERVER[$c]);
}
}
return $_SERVER['REMOTE_ADDR'];
}
?>
modiefied: 20-12-2005
26 comments posted so far
Add your own »
2. On Oct 17 2005 @ 12:47 Niek wrote:
Agreed, I edited the post (it's now a function, no more expensive regexpes and works with E_ALL).3. On Oct 18 2005 @ 05:16 guest wrote:
nice!also i think you're safe to block ips above 224.0.0.0 - and maybe a couple other checks too could help...
http://www.networksorcery.com/enp/protocol/ip.htm under "Internet address block allocation"
or
http://en.wikipedia.org/wiki/Classful_network under "Special Ranges"
otherwise, that was cool you edited it so quick, good work!
4. On Oct 18 2005 @ 14:24 guest wrote:
You should use the BGP and routing bogon list.http://www.cymru.com/Documents/bogon-bn-agg.txt
5. On Nov 29 2005 @ 17:56 Erik wrote:
Here is a new version which works better.I was debugging this function for almost 1 hour to find that a proxy of one of the mininova visitors was returning a HTTP_X_FORWARDED_FOR in the form of: "client ip, other proxy ip" with which the old is_valid_ip would return false.
CODE: PHP
<?
function ip_first($ips) {
if (($pos = strpos($ips, ',')) != false) {
return substr($ips, 0, $pos);
} else {
return $ips;
}
}
function ip_valid($ips) {
if (isset($ips)) {
$ip = ip_first($ips);
$ipnum = ip2long($ip);
if ($ipnum !== -1 && $ipnum !== false && (long2ip($ipnum) === $ip)) { // PHP 4 and PHP 5
if (($ipnum < 167772160 || $ipnum > 184549375) && // Not in 10.0.0.0/8
($ipnum < -1408237568 || $ipnum > -1407188993) && // Not in 172.16.0.0/12
($ipnum < -1062731776 || $ipnum > -1062666241)) // Not in 192.168.0.0/16
return true;
}
}
return false;
}
function ip() {
$check = array('HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED',
'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED',
'HTTP_VIA', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM');
foreach ($check as $c) {
if (ip_valid(&$_SERVER[$c])) {
return ip_first($_SERVER[$c]);
}
}
return $_SERVER['REMOTE_ADDR'];
}
?>
function ip_first($ips) {
if (($pos = strpos($ips, ',')) != false) {
return substr($ips, 0, $pos);
} else {
return $ips;
}
}
function ip_valid($ips) {
if (isset($ips)) {
$ip = ip_first($ips);
$ipnum = ip2long($ip);
if ($ipnum !== -1 && $ipnum !== false && (long2ip($ipnum) === $ip)) { // PHP 4 and PHP 5
if (($ipnum < 167772160 || $ipnum > 184549375) && // Not in 10.0.0.0/8
($ipnum < -1408237568 || $ipnum > -1407188993) && // Not in 172.16.0.0/12
($ipnum < -1062731776 || $ipnum > -1062666241)) // Not in 192.168.0.0/16
return true;
}
}
return false;
}
function ip() {
$check = array('HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED',
'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED',
'HTTP_VIA', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM');
foreach ($check as $c) {
if (ip_valid(&$_SERVER[$c])) {
return ip_first($_SERVER[$c]);
}
}
return $_SERVER['REMOTE_ADDR'];
}
?>
6. On Dec 20 2005 @ 20:06 Erik wrote:
added HTTP_CLIENT_IP header to the checked list.7. On Aug 16 2006 @ 10:03 guest wrote:
This do not work had a scammer from 196.201.076.248 (CLIENT HOST)076 gives invalid result
should make all between seperate numbers to a integer before testing and make 076 to 76
8. On Jul 11 2007 @ 02:10 guest wrote:
i have simmillar problem whith those 2 pop out pages burim not programmer ,please help me to solve this mistery.....on begginer laguage ...i try everything but hopeless...thank mail me at nikolahorvat@beotel.net
Thanks.
9. On May 05 2009 @ 09:56 guest wrote:
When the <a href="http://www.game4power.com/">Wow Gold </a> wolf finally found the <a href="http://www.game4power.com/">Buy Wow Gold</a>hole in the chimney he crawled <a href="http://www.game4power.com/buy-gold/">wow gold cheap </a> down and KERSPLASH right into that kettle of water and that was <a href="http://www.wowgoldone.com/"> cheapest wow gold </a> the end of his troubles with the big bad wolf.<a href="http://www.game4power.com/">game4power</a>,<a href="http://www.game4power.com/buy-gold/">buy cheap wow gold</a>
<a href="http://www.meinwowgold.com/">WOW GOLD</a>
The next day the <a href="http://www.wowgoldone.com/"><strong> cheap wow gold </strong></a><a href="http://www.game4power.com/">buy gold wow</a> little pig invited his mother over . She said "You see it is just as I told you. The way to <a href="http://itemchannel.com">wow gold</a>get along in the world is to do <a href="http://www.itemchannel.com/">world of warcraft gold</a> things as well as you can." Fortunately for that little pig, he <a href="http://www.game4power.com/buy-gold">cheapest wow gold</a> learned that lesson. And he just lived happily ever after!
10. On May 05 2009 @ 09:56 guest wrote:
When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.game4power,buy cheap wow gold
WOW GOLD
The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!
11. On Jun 01 2009 @ 09:47 guest wrote:
you can get following Four series of products information:(jackwalk1985)First,wow gold product:buy wow gold,buy cheap wow gold,and World of Warcraft Gold;
Second wow account product: wow account,buy wow account,sell wow account,World of Warcraft Gold;
Second product:runescape account product runescape account,buy runescape account,sell runescape account,rs account,sell rs account;
Last product:maplestory account maplestory account,maple story account
13. On Jun 22 2009 @ 11:31 guest wrote:
<a href="http://www.rs2baby.com">rs2 money</a><a href="http://www.rs2baby.com">rs2 gold</a>
<a href="http://www.rs2baby.com">rs money</a>
<a href="http://www.rs2baby.com">rs gold</a>
<a href="http://www.rs2baby.com">runescape</a>
<a href="http://www.rs2baby.com">runescape</a>
<a href="http://www.rs2baby.com">runescape gold</a>
<a href="http://www.rs2baby.com">runescape money</a>
<a href="http://www.rs2baby.com">runescap gold</a>
<a href="http://www.rs2baby.com">runescap money</a>
<a href="http://www.hzgames.com">rs2 money</a>
<a href="http://www.hzgames.com">rs2 gold</a>
<a href="http://www.hzgames.com">rs money</a>
<a href="http://www.hzgames.com">rs gold</a>
<a href="http://www.hzgames.com">runescape gold</a>
<a href="http://www.hzgames.com">runescape money</a>
14. On Jun 25 2009 @ 04:13 guest wrote:
DVD Audio RipperFree DVD Audio Ripper
DVD Audio Ripper for Mac
DVD to MP4 Converter
DVD to MP4 Converter for Mac
DVD to iPod Converter
free DVD to iPod Converter
DVD to iPod Converter for Mac
DVD Ripper
Diablo 2 CD Key
Diablo 2 CD Keys
Free DVD Ripper
DVD Ripper for Mac
NFL Jerseys
Scoccer Jerseys
Hockey Jerseys
MLB Jerseys
NHL Jerseys
Nike Sneakers
16. On Jul 03 2009 @ 06:48 www wrote:
When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.game4power,buy cheap wow gold
WOW GOLD
The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!
aion kina.
17. On Jul 05 2009 @ 01:51 www wrote:
<a href="http://www.game4power.com/">game4power</a>,<a href="http://www.game4power.com/buy-gold/">buy cheap wow gold</a><a href="http://www.meinwowgold.com/">WOW GOLD</a>
<a href="http://www.game4power.com/">game4power</a>,<a href="http://www.game4power.com/buy-gold/">buy cheap wow gold</a>
<a href="http://www.meinwowgold.com/">WOW GOLD</a>
<a href="http://www.game4power.com/">game4power</a>,<a href="http://www.game4power.com/buy-gold/">buy cheap wow gold</a>
<a href="http://www.meinwowgold.com/">WOW GOLD</a>
<a href="http://www.game4power.com/">game4power</a>,<a href="http://www.game4power.com/buy-gold/">buy cheap wow gold</a>
<a href="http://www.meinwowgold.com/">WOW GOLD</a>
<a href="http://www.game4power.com/">game4power</a>,<a href="http://www.game4power.com/buy-gold/">buy cheap wow gold</a>
<a href="http://www.meinwowgold.com/">WOW GOLD</a>
18. On Jul 05 2009 @ 01:52 www wrote:
game4power,buy cheap wow goldWOW GOLD
game4power,buy cheap wow gold
WOW GOLD
game4power,buy cheap wow gold
WOW GOLD
game4power,buy cheap wow gold
WOW GOLD
game4power,buy cheap wow gold
WOW GOLD
game4power,buy cheap wow gold
WOW GOLD
19. On Jul 06 2009 @ 08:53 www wrote:
When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.game4power,buy cheap wow gold
WOW GOLD
The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!
aion kina.
20. On Jul 07 2009 @ 10:29 www wrote:
When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.game4power,buy cheap wow gold
WOW GOLD
The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!
aion kina.
21. On Jul 08 2009 @ 09:18 www wrote:
game4power,buy cheap wow goldWOW GOLD
The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!
aion kina.
22. On Jul 09 2009 @ 11:47 www wrote:
When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.game4power,buy cheap wow gold
WOW GOLD
The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!
aion kina.
23. On Jul 11 2009 @ 11:10 www wrote:
game4power,buy cheap wow goldWOW GOLD
The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as buy gold wowI told you. The way to Wow Goldget along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he Cheapest wow Goldlearned that lesson. And he just lived happily ever after!
aion kina.
24. On Jul 14 2009 @ 04:03 guest wrote:
buy wow goldmy wow power leveling
buy wow gold
good wow power leveling
BUY wow gold
my wow power leveling
CHEAP rs gold
cheap wow power leveling
CHEAPEST lotro gold
MY aion gold
buy wow gold
cheap wow gold
CHEAPEST wow gold
25. On Jul 14 2009 @ 05:52 guest wrote:
AVI to DVD Converter,AVI to DVD Creator,iPhone Ringtone Maker for Mac,AVI Converter OS X,VOB Converter OS X,AVCHD Video Converter,FLV Converter,PowerPoint Converter,AVCHD Converter,Blue-Ray ripper,Rip Blue Ray,FLV to MOV Mac,VOB to DVD,HD Video Converter,iPod Playlist Transfer26. On Nov 23 2009 @ 14:19 Rena wrote:
Wow,nice sharing!It is very helpful.
1. On Oct 17 2005 @ 05:24 guest wrote:
imho it should be it's own function, something like real_ip() maybe and return $ip.also, it might produce errors if you have error_reporting(E_ALL) if the indexes on the $_SERVER superglobal are not defined and you're trying to pass it to the is_valid_ip() function. should do an isset() first.
(it also might help to add checks in is_valid_ip() for private netblocks and reject them as being invalid, if you're interested in getting a trackable IP. converting them to an integer ip2long() might be the best, then you can do integer math on them, rather than some regexps to look for 10.x.x.x 192.168.x.x and such)