NameSilo

URL Validation (PHP)

SpaceshipSpaceship
Watch

X_GuardiaN_X

Established Member
Impact
12
I am developing a website that is heavy on domain names, so a good URL validation function is really important. I have searched a lot but haven't been able to find one that seems to work very well.

Can anybody help me out?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains โ€” AI StorefrontUnstoppable Domains โ€” AI Storefront
[FONT="myriad pro,helvetica,Arial"]Are you just verifying the URL exists? If so, you can do that by grabbing the headers. Try:

PHP:
function url_exists($url){
	@$headers=get_headers($url);
    return preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/',$headers[0]);
}

It will return true as long as the URL returns 200 (ok), 301 (permanently redirected), or 302 (temporarily redirected). get_headers is a PHP 5 function.[/FONT]
 
0
•••
if you're trying to validate that the text input by a user really is a url string, you could do something like filter_var($url, FILTER_VALIDATE_URL); assuming you hvae php5
 
0
•••
if you have PEAR maybe it works like this

PHP:
$URLObject = Net_URL($URL);
if($URL == $URLObject->getURL()){
// action if the url is valid
} else {
// action if url is invalid
}

though i'm not sure if it works in PHP 5
 
0
•••
function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}




if(!isValidURL($fldbanner_url))
{
$errMsg .= "* Please enter valid URL including http://<br>";
}
 
Last edited:
0
•••
CatchedCatched
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back