codingtcher.com
Theme Night
Home
Login

Check if a URL is valid

Home » Tutorial Post » Check if a URL is valid
function validationUrl($Url){
    if ($Url == NULL){
        return false;
    }
    $ch = curl_init($Url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return ($httpcode >= 200 && $httpcode < 300) ? true : false;
}

if(validationUrl("http://example.com")=='1'){
 echo "url is valid";
} else{
 echo "url is invalid";
}

$domain="example.com";
if(checkdnsrr($domain,"ANY")){
  echo "url is valid";
} else {
  echo "url is invalid";
}

if(filter_var("http://example.com", FILTER_VALIDATE_URL) === FALSE){
    echo 'Not a valid URL';
} else {
    echo 'Valid URL';
}

Related Post