Lets assume I have the following IP ranges that I want to block
89.96.53.158 and 89.96.53.189
This how am trying to implement it.
My questions: is it the best way to do it as per code below. Is the coding below right for it
$ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));
$start_ip = sprintf('%u', ip2long("89.96.53.158"));
$end_ip = sprintf('%u', ip2long("89.96.53.189"));
// stop only ip range between 89.96.53.158 - 89.96.53.189
if ($ip >= $start_ip && $ip <= $end_ip) {
echo "you cannot access our site";
exit();
}
can I also achieve that using strpos() functions
Yes I figured out another way to do it with strpos() method
if(strpos($_SERVER['REMOTE_ADDR'], "89.96") === 0)
{
echo "you cannot access our site";
exit();
}
If you notice, the === operator makes sure that the 89.96 is at the beginning of the IP address.
This means that you can specify as much of the IP address as you want, and it will block no matter what numbers come after it.
Related
I could not find an example of what I'm trying to accomplish. I guess I don't know the proper search words to use. I have a working script but I want to make it more flexible by adding a main, admin editable config file.
I have the following function:
function ip_is_mobile($ip) {
$pri_addrs = array(
'66.87.0.0-66.87.255.255', // Sprint mobile
'174.192.0.0-174.255.255.255' // Verizon mobile
);
$long_ip = ip2long($ip);
if($long_ip != -1) {
foreach($pri_addrs AS $pri_addr) {
list($start, $end) = explode('-', $pri_addr);
// IF IS a mobile IP
if($long_ip >= ip2long($start) && $long_ip <= ip2long($end))
return true;
}
}
return false;
}
I would like to replace the hard-coded IP address ranges, in the function, with variables or definitions which will be set in the main config file so that the config file has something similar to the following:
// Mobile IP address ranges. Add as many as needed.
$MobileIPs['0']="66.87.0.0-66.87.255.255";
$MobileIPs['1']="174.192.0.0-174.255.255.255";
$MobileIPs['2']="85.110.50.0/24";
My goal is to give the admin an easy to read and understand way of adding as many IP address ranges as necessary (probably 20 max). I'm not opposed to totally rewriting the function if there is a better, more efficient way. In addition to IP ranges, it would be advantageous if CIDR's could also be specified; as indicated in the last code line above.
What edits do I need to make to the function and what would the corresponding lines in the main config file be so that the user can add any number of ranges or CIDR's?
You can store configuration of ip ranges in separate configuration file and use require_once in your main code
ip_ranges.conf.php (configuration file)
<?php
$pri_addrs = array(
'66.87.0.0-66.87.255.255', // Sprint mobile
'174.192.0.0-174.255.255.255' // Verizon mobile
);
index.php (main code file)
function ip_is_mobile($ip) {
require_once(ip_ranges.conf.php); // include config file
$long_ip = ip2long($ip);
if($long_ip != -1) {
foreach($pri_addrs AS $pri_addr) {
list($start, $end) = explode('-', $pri_addr);
// IF IS a mobile IP
if($long_ip >= ip2long($start) && $long_ip <= ip2long($end))
return true;
}
}
return false;
}
I need to check if a file is opened "locally" (same machine or network). I'm using:
<?php
if ((substr($_SERVER['REMOTE_ADDR'],0,8) == "192.168.") || ($_SERVER['REMOTE_ADDR'] == "127.0.0.1")) {
// client is local
} else {
// client is not local
}
But I'm not sure this is the best way.
What is a more foolproof way of doing this?
What Friek said is true, but provided that you know how to get the real client's IP, you can tell if it's a local address using PHP filters:
if ( ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) )
{
// is a local ip address
}
"Foolproof," as always, can be tricky.
If we do restrict ourselves to IPv4, then checking for "127.0.0.1" takes care of the localhost case, but checking against "192.168." is plain wrong - it will only work if the script is being run on a server which happens to be on the 192.168 network, using a 16-bit subnet mask.
Checking $_SERVER['REMOTE_ADDR'] against $_SERVER['SERVER_ADDR'] would be a better bet. This still doesn't take care of the case of a multi-homed host (ie one which has several IP addresses in addition to 127.0.0.1), though.
In order to catch all same-network cases, you'd need to check the combination of SERVER_ADDR and subnet mask against REMOTE_ADDR, but the subnet mask isn't available in $_SERVER.
BUT I found a function which does pretty much what you want here. It's a couple of screens down and it's called clientInSameSubnet. Not my code, but looks right.
In case anyone has trouble finding the above code, suggested by #Uffe, I've included it below:
<?php
/**
* Check if a client IP is in our Server subnet
*
* #param string $client_ip
* #param string $server_ip
* #return boolean
*/
function clientInSameSubnet($client_ip=false,$server_ip=false) {
if (!$client_ip)
$client_ip = $_SERVER['REMOTE_ADDR'];
if (!$server_ip)
$server_ip = $_SERVER['SERVER_ADDR'];
// Extract broadcast and netmask from ifconfig
if (!($p = popen("ifconfig","r"))) return false;
$out = "";
while(!feof($p))
$out .= fread($p,1024);
fclose($p);
// This is to avoid wrapping.
$match = "/^.*".$server_ip;
$match .= ".*Bcast:(\d{1,3}\.\d{1,3}i\.\d{1,3}\.\d{1,3}).*";
$match .= "Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/im";
if (!preg_match($match,$out,$regs))
return false;
$bcast = ip2long($regs[1]);
$smask = ip2long($regs[2]);
$ipadr = ip2long($client_ip);
$nmask = $bcast & $smask;
return (($ipadr & $smask) == ($nmask & $smask));
}
So I want to check if the user is in specific county
$ip = user ip;
$countryIP = some number;
if($ip == $countryIP){echo 'okey';} else {echo 'not okey';}
I dont want to use database with geo location since I need it for only one country.Also i dont want to use third party services.
For example abc.go.com/watch is only for US and I want to do something similar to that
Thanks :)
There is no way to find out if an ip address is assigned to a given country without performing GeoIP on it.
Ok, provided you're happy with checking Bulgaria against the block you indicated, then you can do it reasonably simply - all you need to do is convert the ip addresses to the numerical notation.
77.85.0.0/16 means any code from 77.85.0.0 to 77.85.255.255 - now convert these to a numerical values:
77*256^3+85*256^2+0*256+0 = 1297416192
77*256^3+85*256^2+255*256+255 = 1297481727
Now convert the IP you want to check in the same manner - and verify that it's between these two numbers. Of course, it's not as simple as you are saying. There are over 400 different blocks for Bulgaria. If you care about it all, here's the full list of their numerical representation (as of the last update). Check yourself, for example, here: http://www.countryipblocks.net/country-blocks/select-formats/
# Country: BULGARIA
# ISO Code: BG
# Total Networks: 497
# Total Subnets: 4,148,224
31.13.192.0/18
31.41.16.0/21
31.211.128.0/19
37.60.136.0/21
37.63.0.0/17
37.130.240.0/21
37.157.136.0/21
37.157.160.0/19
46.10.0.0/16
46.16.192.0/21
46.35.160.0/19
46.40.64.0/18
46.47.64.0/18
46.55.128.0/17
46.229.192.0/20
46.232.152.0/21
46.233.0.0/18
46.237.64.0/18
46.238.0.0/18
46.249.64.0/19
46.252.48.0/20
46.253.0.0/20
46.254.128.0/21
62.44.96.0/19
62.73.64.0/18
62.176.64.0/19
62.176.96.0/19
62.182.112.0/21
62.204.128.0/19
62.213.160.0/19
62.221.128.0/19
77.70.0.0/17
77.71.0.0/17
77.76.0.0/18
77.76.128.0/18
77.77.0.0/18
77.77.128.0/18
77.78.0.0/18
77.78.128.0/18
77.85.0.0/16
77.95.232.0/21
77.236.160.0/19
77.238.64.0/19
77.246.208.0/20
78.40.136.0/21
78.83.0.0/16
78.90.0.0/16
78.108.240.0/20
78.128.0.0/17
78.130.128.0/17
78.142.0.0/18
78.154.0.0/19
78.159.128.0/19
79.100.0.0/16
79.110.112.0/20
79.124.0.0/18
79.124.64.0/19
79.132.0.0/19
79.134.32.0/19
79.134.160.0/19
80.72.64.0/20
80.72.80.0/20
80.78.224.0/20
80.80.128.0/20
80.80.144.0/20
80.95.16.0/20
80.253.48.0/20
81.161.240.0/20
82.101.64.0/18
82.103.64.0/18
82.118.224.0/19
82.119.64.0/19
82.137.64.0/18
82.146.0.0/19
82.147.128.0/19
82.199.192.0/19
83.97.24.0/21
83.97.64.0/21
83.142.16.0/21
83.143.144.0/21
83.143.176.0/21
83.143.248.0/21
83.148.64.0/18
83.222.160.0/19
83.228.0.0/17
84.21.192.0/19
84.22.0.0/19
84.38.240.0/20
84.40.64.0/18
84.43.128.0/17
84.54.128.0/18
84.201.192.0/20
84.238.128.0/17
84.242.128.0/18
84.252.0.0/18
85.11.128.0/18
85.14.0.0/18
85.91.128.0/19
85.95.64.0/19
85.118.64.0/19
85.118.192.0/21
85.130.0.0/17
85.187.0.0/16
85.196.128.0/18
85.217.128.0/17
85.239.128.0/19
85.255.128.0/20
85.255.160.0/20
87.97.128.0/17
87.116.64.0/18
87.118.128.0/18
87.119.64.0/18
87.120.0.0/16
87.121.0.0/16
87.126.0.0/16
87.227.128.0/17
87.239.152.0/21
87.246.0.0/18
87.247.248.0/21
87.252.160.0/19
87.254.160.0/19
88.80.96.0/19
88.80.128.0/19
88.87.0.0/19
88.203.128.0/17
88.213.192.0/18
89.25.0.0/17
89.106.96.0/19
89.186.192.0/19
89.190.192.0/19
89.215.0.0/16
89.252.192.0/18
89.253.128.0/18
90.154.128.0/17
91.92.0.0/16
91.134.0.0/16
91.139.128.0/17
91.148.128.0/18
91.191.208.0/20
91.192.236.0/22
91.193.156.0/22
91.193.200.0/22
91.195.24.0/23
91.196.64.0/22
91.196.124.0/22
91.196.224.0/22
91.198.119.0/24
91.198.132.0/24
91.198.181.0/24
91.198.228.0/24
91.199.36.0/24
91.199.128.0/24
91.199.150.0/24
91.199.237.0/24
91.199.247.0/24
91.201.172.0/22
91.204.156.0/22
91.206.20.0/23
91.206.138.0/23
91.207.190.0/23
91.209.8.0/24
91.209.21.0/24
91.209.146.0/24
91.210.88.0/22
91.211.108.0/22
91.211.188.0/22
91.211.232.0/22
91.212.13.0/24
91.212.17.0/24
91.212.37.0/24
91.212.163.0/24
91.212.201.0/24
91.212.233.0/24
91.212.235.0/24
91.213.12.0/24
91.215.216.0/22
91.216.71.0/24
91.216.95.0/24
91.216.174.0/24
91.216.253.0/24
91.217.148.0/24
91.217.205.0/24
91.217.215.0/24
91.218.80.0/22
91.220.176.0/24
91.220.189.0/24
91.221.254.0/23
91.222.20.0/22
91.223.66.0/24
91.226.226.0/23
91.228.38.0/24
91.230.6.0/23
91.230.192.0/22
91.230.230.0/24
91.230.231.0/24
91.232.209.0/24
91.233.32.0/24
91.234.19.0/24
91.234.21.0/24
91.234.92.0/22
91.235.164.0/24
91.235.248.0/22
91.236.144.0/22
91.237.56.0/22
91.237.102.0/23
92.62.240.0/20
92.247.0.0/16
93.93.8.0/21
93.94.136.0/21
93.123.0.0/17
93.152.128.0/17
93.155.128.0/17
93.183.128.0/18
94.26.0.0/17
94.72.128.0/18
94.73.0.0/18
94.101.192.0/20
94.139.192.0/19
94.155.0.0/16
94.156.0.0/16
94.190.128.0/17
94.236.128.0/17
95.42.0.0/16
95.43.0.0/16
95.87.0.0/18
95.87.192.0/18
95.111.0.0/17
95.140.208.0/20
95.158.128.0/18
95.168.224.0/19
95.169.192.0/19
109.104.192.0/19
109.107.64.0/19
109.120.192.0/18
109.121.128.0/18
109.121.192.0/18
109.160.0.0/17
109.199.128.0/19
109.199.224.0/19
128.140.176.0/20
130.185.192.0/18
130.204.0.0/16
145.255.192.0/19
149.62.192.0/18
158.58.192.0/18
164.138.216.0/21
176.12.0.0/18
176.67.232.0/21
176.222.0.0/20
178.16.128.0/20
178.75.192.0/18
178.132.80.0/21
178.169.128.0/17
178.239.112.0/20
178.239.224.0/20
178.249.168.0/21
178.254.192.0/18
188.124.64.0/19
188.126.0.0/19
188.127.64.0/19
188.254.128.0/17
192.58.32.0/22
192.92.129.0/24
192.162.231.0/24
193.9.16.0/24
193.16.102.0/24
193.16.157.0/24
193.16.246.0/24
193.17.229.0/24
193.19.172.0/22
193.22.103.0/24
193.22.248.0/24
193.23.52.0/24
193.24.240.0/22
193.25.162.0/23
193.26.14.0/24
193.26.216.0/24
193.28.250.0/24
193.29.55.0/24
193.30.228.0/22
193.36.35.0/24
193.37.238.0/24
193.41.64.0/22
193.41.182.0/23
193.41.188.0/22
193.43.26.0/24
193.47.74.0/24
193.68.0.0/19
193.68.96.0/19
193.68.128.0/17
193.84.86.0/24
193.93.24.0/22
193.104.79.0/24
193.104.165.0/24
193.105.60.0/24
193.105.148.0/24
193.105.151.0/24
193.105.196.0/24
193.107.36.0/22
193.107.68.0/22
193.108.24.0/24
193.108.32.0/23
193.109.54.0/23
193.110.82.0/24
193.110.132.0/24
193.110.159.0/24
193.110.216.0/21
193.111.89.0/24
193.111.194.0/23
193.138.67.0/24
193.142.0.0/24
193.150.67.0/24
193.151.20.0/22
193.151.80.0/22
193.160.159.0/24
193.161.192.0/24
193.164.222.0/23
193.169.198.0/23
193.178.152.0/23
193.178.166.0/24
193.178.222.0/24
193.186.38.0/24
193.192.48.0/23
193.192.56.0/23
193.193.162.0/23
193.193.164.0/24
193.193.182.0/24
193.194.140.0/23
193.194.156.0/24
193.200.1.0/24
193.200.2.0/24
193.200.8.0/24
193.200.12.0/24
193.200.14.0/23
193.200.16.0/23
193.200.24.0/24
193.200.28.0/24
193.201.114.0/23
193.201.172.0/24
193.201.240.0/22
193.202.108.0/24
193.203.198.0/23
193.218.0.0/24
193.228.152.0/24
193.254.29.0/24
194.0.32.0/24
194.0.235.0/24
194.8.4.0/24
194.8.53.0/24
194.8.60.0/24
194.9.6.0/23
194.12.224.0/19
194.34.12.0/24
194.36.167.0/24
194.48.206.0/24
194.50.73.0/24
194.50.76.0/24
194.50.122.0/24
194.54.140.0/22
194.63.136.0/22
194.79.12.0/22
194.88.228.0/23
194.88.250.0/23
194.110.205.0/24
194.141.0.0/19
194.141.32.0/19
194.141.64.0/18
194.141.128.0/17
194.145.63.0/24
194.145.160.0/22
194.146.232.0/22
194.150.116.0/22
194.150.180.0/23
194.153.118.0/24
194.153.145.0/24
194.169.231.0/24
194.187.132.0/22
194.246.110.0/23
195.8.222.0/23
195.10.193.0/24
195.20.20.0/22
195.20.24.0/22
195.22.146.0/23
195.24.32.0/19
195.24.88.0/21
195.28.6.0/23
195.34.96.0/19
195.35.84.0/24
195.39.198.0/23
195.39.212.0/23
195.42.142.0/23
195.47.193.0/24
195.54.44.0/23
195.62.22.0/23
195.64.160.0/23
195.66.125.0/24
195.68.200.0/23
195.68.214.0/23
195.69.108.0/22
195.69.120.0/22
195.69.164.0/22
195.72.112.0/24
195.74.85.0/24
195.85.215.0/24
195.88.74.0/23
195.88.140.0/23
195.96.224.0/19
195.110.24.0/23
195.114.112.0/23
195.128.132.0/24
195.128.134.0/24
195.128.224.0/23
195.137.252.0/23
195.138.128.0/19
195.149.71.0/24
195.149.248.0/21
195.162.72.0/23
195.170.166.0/24
195.170.178.0/24
195.177.218.0/23
195.177.248.0/23
195.178.8.0/23
195.178.98.0/23
195.178.116.0/23
195.182.41.0/24
195.182.44.0/24
195.189.80.0/22
195.189.224.0/23
195.191.34.0/23
195.191.60.0/23
195.191.94.0/23
195.191.148.0/23
195.214.248.0/21
195.216.228.0/24
195.225.51.0/24
195.225.124.0/22
195.225.252.0/22
195.230.0.0/19
195.234.84.0/22
195.234.236.0/22
195.238.84.0/23
195.242.106.0/23
195.242.126.0/23
195.242.240.0/22
195.246.240.0/23
212.5.32.0/19
212.5.128.0/19
212.21.128.0/19
212.25.32.0/19
212.36.0.0/19
212.39.64.0/19
212.43.32.0/19
212.45.64.0/19
212.50.0.0/19
212.50.64.0/19
212.56.0.0/19
212.70.128.0/19
212.72.192.0/19
212.73.128.0/19
212.75.0.0/19
212.91.160.0/19
212.95.160.0/19
212.104.96.0/19
212.116.128.0/19
212.117.32.0/19
212.122.160.0/19
212.124.64.0/19
212.233.128.0/17
213.16.32.0/19
213.91.128.0/17
213.108.240.0/21
213.130.64.0/19
213.137.32.0/19
213.145.96.0/19
213.149.128.0/19
213.167.0.0/19
213.169.32.0/19
213.191.192.0/19
213.214.64.0/19
213.222.32.0/19
213.226.0.0/19
213.226.32.0/19
213.231.128.0/18
213.240.192.0/18
217.9.224.0/20
217.10.240.0/20
217.18.240.0/20
217.30.208.0/20
217.75.128.0/20
217.75.144.0/20
217.79.32.0/20
217.79.64.0/20
217.79.80.0/20
217.145.80.0/20
217.145.160.0/20
217.174.48.0/20
217.174.144.0/20
another way
$continent = geoip_continent_code_by_name('www.example.com');
if ($continent) {
echo 'This host is located in: ' . $continent;
}
http://www.php.net/manual/en/function.geoip-continent-code-by-name.php
Ok, here's what I'm looking for: from a list of links, I'm stripping everything but the domains. The result is a mixed list of domains and domain-names which represent subdomains.
stackoverflow.com
security.stackexchange.com
whoknows.test.co.uk
you.get.it.by.now.dont.you.com
What I want to do is to trim all list entries down to their VALID (=only existing) root domains like this:
stackoverflow.com
security.stackexchange.com
test.co.uk
-fail-
Currently I explode each line into an array and work my list from back to front, using curl to check each potential root domain for it's existance... as soon as curl throws back a HTTP code >= 200 and < 400, I regard the root domain to be found. When the end of each potential domain lookup is done and no valid domain has been found at all, the domain is considered to be non-existant.
input: stackoverflow.com
test: stackoverflow.com - succeeds and is the root domain
result: stackoverflow.com - valid root domain
input: whoknows.test.co.uk
test: co.uk - fails
test: test.co.uk - succeeds (theoretically) and is the root domain
result: test.co.uk - valid root domain
input: you.get.it.by.now.dont.you.com
test: you.com - fails
test: dont.you.com - fails
test: now.dont.you.com - fails
test: by.now.dont.you.com - fails
test: it.by.now.dont.you.com - fails
test: get.it.by.now.dont.you.com - fails
test: you.get.it.by.now.dont.you.com - fails
result: you.get.it.by.now.dont.you.com - invalid domain
Is there any alternative way to do this? I would like to stop heating up my webserver's CPU with 2 to X (=near to unlimited) curl look-ups for every domain on my 100.000+ list. Also, all these lookups take a bunch of time. Maybe - so I hope - there is a quicker solution to do this.
The catch? It has to work with PHP.
There are a bunch of shortcuts to acheive what you need.
For example, you already know that .co.uk and .com are TLDs, so checking these you can obviously skip.
The problem is with all the other crazy TLDs.
I suggest you take a look at the source for ruby-domain-name
They have done a lot of work using RFCs and known data, to try and process it the right way.
So...
I've been fiddling around with this for a long time now, looking for the potential bottlenecks and after a few days of back and forth I discovered that it's actually CURL (that's waiting for the individual servers to respond with a HTTP code) that's making things slower than needed.
In the end, I opted in for a different "gethostbyname" function that takes IP6 into account to solve my problem(s).
function my_gethostbyname($host, $try_a = FALSE)
{
$dns = gethostbynamel6($host, $try_a);
if ($dns == FALSE)
{
return FALSE;
}
else
{
return $dns[0];
}
}
function gethostbynamel6($host, $try_a = FALSE)
{
$dns = array();
$dns6 = #dns_get_record($host, DNS_AAAA);
if($dns6!== FALSE)
{
$dns = array_merge($dns, $dns6);
}
if ($try_a == TRUE)
{
$dns4 = #dns_get_record($host, DNS_A);
if($dns4!== FALSE)
{
$dns = array_merge($dns, $dns4);
}
}
else
{
$dns = $dns6;
}
$ip6 = array();
$ip4 = array();
foreach ($dns as $record)
{
if ($record["type"] == "A")
{
$ip4[] = $record["ip"];
}
if ($record["type"] == "AAAA")
{
$ip6[] = $record["ipv6"];
}
}
if (count($ip6) < 1)
{
if ($try_a == TRUE)
{
if (count($ip4) < 1)
{
return FALSE;
}
else
{
return $ip4;
}
}
else
{
return FALSE;
}
}
else
{
return $ip6;
}
}
As soon as the first domain-part actually resolves to an IP, (a) the domain exists and (b) is the root domain.
This spares me major time and the trick of this is that you're only as slow as your DNS resolution and some microsecs. The curl option I used before took around 3 seconds per call (sometimes up to the full timeout range I had set to 20 secs), depending on the target server's response time - if any.
The path I now chose is easy to understand: I end up with a list of resolving domains and - when needed - I can check those using curl "on demand" or using one or more CRON jobs "on interval".
I know that it's kind of a workaround, but splitting the problem into two tasks (1 = pre-check for root domain, 2 = check if domain returns expected HTTP code) makes the whole thing faster than trying to do the complete job at once using curl.
What I've learned from this...
When checking domains, try to resolve them first so you can spare yourself the timeout burden that comes with curl.
Curl is great for many tasks, but it's not the smartest way to try to do everything with it.
When you think you can't solve a problem more than you've tried to do, split the problem in two or more parts and check again. Chances are big that you'll discover a whole new world of options to enhance what you've got.
I hope this spares someone the burden of fiddling around with an alike problem for weeks. ;)
class DomainUtils {
function __construct(){
//only these super domains
$this->superDomains = array(
'.com',
'.gov',
'.org',
'.co.uk',
'.info',
'.co',
'.net',
'.me',
'.tv',
'.mobi'
);
}
//get super domain
public function getMainDomain($domain = null){
$domainChunks = explode('.', str_ireplace($this->superDomains, '', $domain));
if(sizeof($domainChunks) == 0){
return false;
}
foreach($domainChunks as $key => $domainChunk){
if($key < sizeof($domainChunks) - 1){
$domain = str_ireplace($domainChunk . '.', '', $domain);
}
}
return $domain;
}
}
Actually this is a two parts question:
First, is ip2long a good IP validator? Suppose someone inserts an IP in a form, and I want to validate that it is correct. Is it OK to just check that ip2long doesn't return FALSE ?
Second: What do you think about checking the IP of the visitors, and denying access if it's not a valid IP? Looking at my visitor's ips, sometimes I find things like "1.1 TIBURON".. Wtf is that? I heard the expression 'spoofed ip', is that what it is? Is it related to spam bots?
If you just need to valdiate that the IP address is correct in format you can use a regular expresión like the one bellow.
EDIT:
if (preg_match('/\A(?:^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)\Z/im', $subject)) {
# Successful match
} else {
# Match attempt failed
}
If you want to go further you can do a ping to the IP to discover if it is active.
About your second question, i don't know what to say, i've never seen the "1.1 TIBURON" thing,
HTH
Depends on how thorough you want the validation to be. A very simple check for well-formed IP addresses would be a regex along the lines of /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/. (Actually, SubniC's regex is probably much better). This would catch your 1.1. TIBURON entry, because it's not well-formed.
I'm not sure whether ip2long checks this or just silently discards the part of the string that doesn't look like an IP.
One step further you could block 'reserved' IP addresses and ranges, such as 127.0.0.1, 255.255.255.255, etc.
You might also want to add a blacklist to filter out requests from dubious networks, or from clients you have had trouble with in the past.
I'm curious though how that entry got there in the first place - an attacker shouldn't be able to get this value into $_SERVER['REMOTE_ADDR'] unless they have compromised the underlying OS, or at least apache (or whatever web server you're running).
You're not by any chance reading the IP on the client (using javascript)?
I had this code around. It's been a while and I can't quite remember some of the decisions behind it, but I assure you it has been thoroughly tested
(actually I just tweaked it a little, renaming variables and adding comments, so it's possible I just broke it :)
You may find the get_ip_addr and is_private_ip_addr functions useful.
get_ip_addr can take both an IP address or a host name.
<?php
function get_ip_addr($host){
$long_ipaddr = my_ip2long($host);
$host = trim($host);
if( $long_ipaddr !== false ){
$str_ip = long2ip($long_ipaddr); // Because of e.g. 245.254.245
return $str_ip;
}
$ip_addr = #gethostbyname($host);
if( $ip_addr and $ip_addr != -1 ){
return $ip_addr;
}
return false;
}
function my_ip2long($ipaddr){
$long_ip = ip2long($ipaddr);
if( $long_ip === false ){
return false;
}
// http://php.net/manual/en/function.ip2long.php says:
// Note:
// Because PHP's integer type is signed, and many IP addresses
// will result in negative integers, you need to use
// the "%u" formatter of sprintf() or printf() to get
// the string representation of the unsigned IP address.
$long_ip = sprintf("%u", $long_ip);
return $long_ip;
}
function ip2bin($ip){
$octets = explode(".", $ip);
foreach($octets as $k => $v){
$octets[$k] = str_pad(decbin($v), 8, "0", STR_PAD_LEFT);
}
return implode('', $octets);
}
function ip_in_range($ip, $prefix, $mask_len){
$ip = ip2bin($ip);
$prefix = ip2bin($prefix);
$ip = substr($ip, 0, $mask_len);
$prefix = substr($prefix, 0, $mask_len);
// Watch out! Two numerical strings are converted to integers
// when you use ==. This is trouble for long integers.
// Using === skips this behaviour
return ($ip === $prefix);
}
function is_private_ip_addr($ipaddr){
if( "localhost" === $ipaddr ) return true;
$long_ipaddr = my_ip2long($ipaddr);
if( $long_ipaddr === false ){
return false; // Shouldn't be calling this!
}
// Info below obtained from http://bugs.php.net/bug.php?id=47435#c145655
// Not sure why 127.0.0.0/8 isn't mentioned ...?
// Also, in IPv6 there's the multicast address range: ff00::/8s
//
// IPv4 private ranges
// 10.0.0.0/8 // private use network (rfc1918)
// 172.16.0.0/12 // private use network (rfc1918)
// 192.168.0.0/16 // private use network (rfc1918)
//
// IPv4 reserved ranges
// 0.0.0.0/8 // "this" network (rfc1700)
// 169.254.0.0/16 // link local network (rfc3927)
// 192.0.2.0/24 // test net (rfc3330)
// 224.0.0.0/4 // Multicast (rfc3171)
// 240.0.0.0/4 // Reserved for Future Use (rfc1700)
//
// IPv6 Private range
// fc00::/7 // unique-local addresses (rfc4193)
//
// IPv6 Reserved ranges
// ::/128 // unspecified address (rfc4291)
// ::1/128 // loopback address (rfc4291)
// fe80::/10 // link local unicast (rfc4291)
// 2001:db8::/32 // documentation addresses (rfc3849)
// 5f00::/8 // 6Bone
// 3ffe::/16 // 6Bone
// ::ffff:0:0/96 // IPv4-Mapped addresses (rfc4291)
// 2001:10::/28 // ORCHID addresses (rfc4843)
// ::/0 // default unicast route address
//
// Anyways, this are the relevant RFCs:
// RFC 3330 (Sep 2002), "Special-Use IPv4 Addresses":
// see section 3 for a nice table
// RFC 5156 (Apr 2008), "Special-Use IPv6 Addresses"
//
// Also, this function currently only deals with IPv4 addresses,
// since PHP's ip2long simply doesn't support IPv6 anyway.
// You can't currently even trust long ints to have 64 bit,
// so a 128 bit long int is out of the question.
if( ip_in_range($ipaddr, "127.0.0.0", 8) ) return true; // Loopback
if( ip_in_range($ipaddr, "10.0.0.0", 8) ) return true; // Private addresses (Class A range)
if( ip_in_range($ipaddr, "172.16.0.0", 12) ) return true; // Private addresses (Class B range)
if( ip_in_range($ipaddr, "192.168.0.0", 16) ) return true; // Private addresses (Class C range)
if( ip_in_range($ipaddr, "169.254.0.0", 16) ) return true; // "This" network
if( ip_in_range($ipaddr, "192.0.2.0", 24) ) return true; // "TEST-NET" (documentation and examples)
if( ip_in_range($ipaddr, "224.0.0.0", 4) ) return true; // Multicast
if( ip_in_range($ipaddr, "240.0.2.0", 4) ) return true; // Reserved for future use
return false;
}
You can consider this code released under a permissive CC BY-SA.
Do whatever you want with it, but if you improve on it please tells us about it here.
I'm marking this post community wiki..