I was searching to make a script that validates IP address and hostname and I found many different solutions, none of which good and working until I found a very old php3 project. I worked pretty much to adapt it and I thought that I may share it (not that somebody will need it LOL):
<?php
// IP address is valid, and resolves to a hostname? true or false
function is_ip($IP){
if(empty($IP)) return false;
// does it look like IP?
if( ! is_ipaddress($IP)) return false;
// enable validation by looking for DNS records!
//if (! checkdnsrr($IP, "ANY")) return false;
// finally the IP looks OK
return true;
}
// if a string "looks loke" an IP
function is_ipaddress ($IP){
if(empty($IP)) return false;
// 123456789012345
// xxx.xxx.xxx.xxx
$len = strlen($IP);
if( $len > 15 ) return false;
$Bad = eregi_replace("([0-9\.]+)","",$IP);
// contains bad symbols?
if( ! empty($Bad)) return false;
// four octets?
$chunks = explode(".",$IP);
$count = count($chunks);
if ($count != 4) return false;
// 0 < octet < 256 check
while ( list ($key,$val) = each ($chunks) ){
if(ereg("^0",$val)) return false;
$Num = $val;
settype($Num,"integer");
if($Num > 255) return false;
}
return true;
}
// Hostname is a reachable internet host? true or false
function is_host ($hostname)
{
if(empty($hostname)) return false;
if( ! is_hostname($hostname)) return false;
// enable for validation for existing dns records
//if( ! checkdnsrr($hostname,"ANY")) return false;
return true;
}
function is_hostname ($hostname)
{
// make sure that it does not have bad characters
$bad = eregi_replace("[-A-Z0-9\.]","",$host);
if ( ! empty($bad)) return false;
// no .. in hostnames!
if(ereg("\.\.",$hostname)) return false;
// no leading dot (.) in hostnames!
if(ereg("^\.",$hostname)) return false;
// dot seperator check
$chunks = explode(".",$hostname);
if( (gettype($chunks)) != "array") return false;
$count = (count($chunks)-1);
if($count < 1) return false;
// see if we're doing www.hostname.tld or hostname.tld
if(eregi("^www\.",$hostname)) $web = true;
// and do a security check to prevent queries to www.com, www.net,...
if( ($web) and ($count < 2) ) return false;
// now we will see if the hostname has tld (.com, .net, etc) at the end
$tld = $chunks[$count];
if(empty($tld)) return false;
// and we will see if it is valid tld of course
if( ! is_gTLD($tld)) {
if( ! is_ccTLD($tld)) return false;
}
// finally we have a valid hostname
return true;
}
// checks for most common tlds in order to save time
function is_gTLD ($tld)
{
if(empty($tld))
{
return false;
}
if(eregi("^\.",$tld))
{
$tld = eregi_replace("^\.","",$tld);
}
// there are more than four gTLD's but I was lazy to rename the variable :)
$BigFour = array (
aero=>aero,
asia=>asia,
biz=>biz,
cat=>cat,
com=>com,
coop=>coop,
edu=>edu,
gov=>gov,
info=>info,
int=>int,
jobs=>jobs,
mil=>mil,
mobi=>mobi,
museum=>museum,
name=>name,
net=>net,
org=>org,
pro=>pro,
tel=>tel,
travel=>travel
);
$tld = strtolower($tld);
if(isset($BigFour[$tld]))
{
unset($BigFour);
return true;
}
unset($BigFour);
return false;
}
function is_ccTLD ($countrycode)
{
if(empty($countrycode)) return false;
$countrycode = strtolower($countrycode);
// country codes are two letters only
if( (strlen($countrycode)) != 2 ) return false;
// Huge array. Up-to-date until end 2007 by the official registry
$CCodes = array (
ac => "Ascension Island",
ad => "Andorra",
ae => "United Arab Emirates",
af => "Afghanistan",
ag => "Antigua and Barbuda",
ai => "Anguilla",
al => "Albania",
am => "Armenia",
an => "Netherlands Antilles",
ao => "Angola",
aq => "Antarctica",
ar => "Argentina",
"as" => "American Samoa",
at => "Austria",
au => "Australia",
aw => "Aruba",
ax => "Aland Islands",
az => "Azerbaijan",
ba => "Bosnia and Herzegovina",
bb => "Barbados",
bd => "Bangladesh",
be => "Belgium",
bf => "Burkina Faso",
bg => "Bulgaria",
bh => "Bahrain",
bi => "Burundi",
bj => "Benin",
bl => "Saint Barthelemy",
bm => "Bermuda",
bn => "Brunei Darussalam",
bo => "Bolivia",
br => "Brazil",
bs => "Bahamas",
bt => "Bhutan",
bv => "Bouvet Island",
bw => "Botswana",
by => "Belarus",
bz => "Belize",
ca => "Canada",
cc => "Cocos (Keeling) Islands",
cd => "Congo, The Democratic Republic of the",
cf => "Central African Republic",
cg => "Congo, Republic of",
ch => "Switzerland",
ci => "Cote d'Ivoire",
ck => "Cook Islands",
cl => "Chile",
cm => "Cameroon",
cn => "China",
co => "Colombia",
cr => "Costa Rica",
cu => "Cuba",
cv => "Cape Verde",
cx => "Christmas Island",
cy => "Cyprus",
cz => "Czech Republic",
de => "Germany",
dj => "Djibouti",
dk => "Denmark",
dm => "Dominica",
"do" => "Dominican Republic",
dz => "Algeria",
ec => "Ecuador",
ee => "Estonia",
eg => "Egypt",
eh => "Western Sahara",
er => "Eritrea",
es => "Spain",
et => "Ethiopia",
eu => "European Union",
fi => "Finland",
fj => "Fiji",
fk => "Falkland Islands (Malvinas)",
fm => "Micronesia, Federated States of",
fo => "Faroe Islands",
fr => "France",
ga => "Gabon",
gb => "United Kingdom",
gd => "Grenada",
ge => "Georgia",
gf => "French Guiana",
gg => "Guernsey",
gh => "Ghana",
gi => "Gibraltar",
gl => "Greenland",
gm => "Gambia",
gn => "Guinea",
gp => "Guadeloupe",
gq => "Equatorial Guinea",
gr => "Greece",
gs => "South Georgia and the South Sandwich Islands",
gt => "Guatemala",
gu => "Guam",
gw => "Guinea-Bissau",
gy => "Guyana",
hk => "Hong Kong",
hm => "Heard and McDonald Islands",
hn => "Honduras",
hr => "Croatia/Hrvatska",
ht => "Haiti",
hu => "Hungary",
id => "Indonesia",
ie => "Ireland",
il => "Israel",
im => "Isle of Man",
in => "India",
io => "British Indian Ocean Territory",
iq => "Iraq",
ir => "Iran, Islamic Republic of",
is => "Iceland",
it => "Italy",
je => "Jersey",
jm => "Jamaica",
jo => "Jordan",
jp => "Japan",
ke => "Kenya",
kg => "Kyrgyzstan",
kh => "Cambodia",
ki => "Kiribati",
km => "Comoros",
kn => "Saint Kitts and Nevis",
kp => "Korea, Democratic People's Republic",
kr => "Korea, Republic of",
kw => "Kuwait",
ky => "Cayman Islands",
kz => "Kazakhstan",
la => "Lao People's Democratic Republic",
lb => "Lebanon",
lc => "Saint Lucia",
li => "Liechtenstein",
lk => "Sri Lanka",
lr => "Liberia",
ls => "Lesotho",
lt => "Lithuania",
lu => "Luxembourg",
lv => "Latvia",
ly => "Libyan Arab Jamahiriya",
ma => "Morocco",
mc => "Monaco",
md => "Moldova, Republic of",
me => "Montenegro",
mf => "Saint Martin (French part)",
mg => "Madagascar",
mh => "Marshall Islands",
mk => "Macedonia, The Former Yugoslav Republic of",
ml => "Mali",
mm => "Myanmar",
mn => "Mongolia",
mo => "Macao",
mp => "Northern Mariana Islands",
mq => "Martinique",
mr => "Mauritania",
ms => "Montserrat",
mt => "Malta",
mu => "Mauritius",
mv => "Maldives",
mw => "Malawi",
mx => "Mexico",
my => "Malaysia",
mz => "Mozambique",
na => "Namibia",
nc => "New Caledonia",
ne => "Niger",
nf => "Norfolk Island",
ng => "Nigeria",
ni => "Nicaragua",
nl => "Netherlands",
no => "Norway",
np => "Nepal",
nr => "Nauru",
nu => "Niue",
nz => "New Zealand",
om => "Oman",
pa => "Panama",
pe => "Peru",
pf => "French Polynesia",
pg => "Papua New Guinea",
ph => "Philippines",
pk => "Pakistan",
pl => "Poland",
pm => "Saint Pierre and Miquelon",
pn => "Pitcairn Island",
pr => "Puerto Rico",
ps => "Palestinian Territory, Occupied",
pt => "Portugal",
pw => "Palau",
py => "Paraguay",
qa => "Qatar",
re => "Reunion Island",
ro => "Romania",
rs => "Serbia",
ru => "Russian Federation",
rw => "Rwanda",
sa => "Saudi Arabia",
sb => "Solomon Islands",
sc => "Seychelles",
sd => "Sudan",
se => "Sweden",
sg => "Singapore",
sh => "Saint Helena",
si => "Slovenia",
sj => "Svalbard and Jan Mayen Islands",
sk => "Slovak Republic",
sl => "Sierra Leone",
sm => "San Marino",
sn => "Senegal",
so => "Somalia",
sr => "Suriname",
st => "Sao Tome and Principe",
su => "Soviet Union (being phased out)",
sv => "El Salvador",
sy => "Syrian Arab Republic",
sz => "Swaziland",
tc => "Turks and Caicos Islands",
td => "Chad",
tf => "French Southern Territories",
tg => "Togo",
th => "Thailand",
tj => "Tajikistan",
tk => "Tokelau",
tl => "Timor-Leste",
tm => "Turkmenistan",
tn => "Tunisia",
to => "Tonga",
tp => "East Timor",
tr => "Turkey",
tt => "Trinidad and Tobago",
tv => "Tuvalu",
tw => "Taiwan",
tz => "Tanzania",
ua => "Ukraine",
ug => "Uganda",
uk => "United Kingdom",
um => "United States Minor Outlying Islands",
us => "United States",
uy => "Uruguay",
uz => "Uzbekistan",
va => "Holy See (Vatican City State)",
vc => "Saint Vincent and the Grenadines",
ve => "Venezuela",
vg => "Virgin Islands, British",
vi => "Virgin Islands, US",
vn => "Vietnam",
vu => "Vanuatu",
wf => "Wallis and Futuna Islands",
ws => "Samoa",
ye => "Yemen",
yt => "Mayotte",
yu => "Yugoslavia (being phased out)",
za => "South Africa",
zm => "Zambia",
zw => "Zimbabwe"
);
if( ! isset($CCodes[$countrycode])) return false;
unset($CCodes);
return true;
}
?>