Is this Input a Valid URL?
This function is in both Classic ASP and PHP
I was visiting microsoft.public.inetserver.asp.general and someone had posted wanting to know how to validate if input received was a valid url. I thought this was something I might want to use, and then decided it was something I could share with the community. There are both an ASP Classic version and a PHP version available.
Classic ASP Verion
If you want, you can download the ASP code, or copy and paste from below.
function isvalidurl(uri)
'change the case to make things easier
uri = lcase(uri)
'if the uri starts with the protocol identifier and if so remove it
if left(uri,7) = "http://" then
uri = mid(uri,8)
else
uri = uri
end if
'check for a trailing slash, then remove it
if instr(uri,"/") > 0 then
uri = left(uri,instrrev(uri,"/")-1)
else
uri = uri
end if
uriarr = split(uri,".")
tld = uriarr(ubound(uriarr))
'this is only the general tlds and does not include the country tlds
tldarr = array("aero", "arpa", "asia", "biz", "cat", "com", "coop", "edu", "gov", "info", "int", "jobs", "mil", "mobi", "museum", "name", "net", "org", "pro", "tel", "travel")
for i = 0 to ubound(tldarr)
if tld = tldarr(i) then
isurl = true
exit for
else
isurl = false
end if
next
isvalidurl = isurl
PHP Version
For those using PHP, it's even easier! Download the PHP code or copy and paste below.
function validurl($uri)
{
if(substr($uri,7)=="http://"||substr($uri,8)=="https://")
{$ok = true;}
$uriarr = explode($uri,".");
$tld = count($ariarr);
$tldarr = array("aero", "arpa", "asia", "biz", "cat", "com", "coop", "edu", "gov", "info", "int", "jobs", "mil", "mobi", "museum", "name", "net", "org", "pro", "tel", "travel");
if(in_array($tld,$tldarr))
{$ok = true;}
if($ok)
{return true;}
else
{return false;}
}
Enjoy and happy coding!



