简体   繁体   中英

Check both valid URL and valid Domain Name

i am using the following function, this gives me the right output but ONLY it checks is URL pattern not correct Domain name...

    filter_var($url, FILTER_VALIDATE_URL)

If i'll enter the correct URL it displays it is valid but if i'll enter the correct URL but not correct Domain name still it is displays the Valid URL..

Ex.
    http://www.google.co.in
    Output: Valid

    http://www.google
    output: Invalid

    http://www.google.aa
    output: Valid

In the third case it should be Invalid...

Any references would be appreciated...

Try this out

function url_exist($url){//se passar a URL existe
    $c=curl_init();
    curl_setopt($c,CURLOPT_URL,$url);
    curl_setopt($c,CURLOPT_HEADER,1);//get the header
    curl_setopt($c,CURLOPT_NOBODY,1);//and *only* get the header
    curl_setopt($c,CURLOPT_RETURNTRANSFER,1);//get the response as a string from curl_exec(), rather than echoing it
    curl_setopt($c,CURLOPT_FRESH_CONNECT,1);//don't use a cached version of the url
    if(!curl_exec($c)){
        //echo $url.' inexists';
        return false;
    }else{
        //echo $url.' exists';
        return true;
    }
    //$httpcode=curl_getinfo($c,CURLINFO_HTTP_CODE);
    //return ($httpcode<400);
}

Technically the second example should also be considered 'valid' and I am surprised that the filter does not validate it as correct. The third example is also correct. That method checks for syntax only and all three examples are actually correct syntax for a URL.

But you're on a right path here, don't get discouraged by what the filter check does. This is what I do to validate domains:

  • Check validity of syntax (filter_var($url,FILTER_VALIDATE_URL) is appropriate here)
  • Check existence of domain DNS (PHP functions gethostbyname() and dns_get_record() both help here)

Do note that it is recommended not to outright 'fail' the user if the second step fails. Sometimes there are problems with DNS or the server and the request may fail despite being correct (even facebook.com can fail at times). As a result you should 'allow' the URL, but not do anything with it until you have double-checked it again at later time. Thus, if multiple checks fail then you should cancel the process.

Hope this works for you!

/**
 * checks if a domain name is valid
 * @param  string $domain_name 
 * @return bool              
 */
public function validate_domain_name($domain_name)
{
    //FILTER_VALIDATE_URL checks length but..why not? so we dont move forward with more expensive operations
    $domain_len = strlen($domain_name);
    if ($domain_len < 3 OR $domain_len > 253)
        return FALSE;

    //getting rid of HTTP/S just in case was passed.
    if(stripos($domain_name, 'http://') === 0)
        $domain_name = substr($domain_name, 7); 
    elseif(stripos($domain_name, 'https://') === 0)
        $domain_name = substr($domain_name, 8);

    //we dont need the www either                 
    if(stripos($domain_name, 'www.') === 0)
        $domain_name = substr($domain_name, 4); 

    //Checking for a '.' at least, not in the beginning nor end, since http://.abcd. is reported valid
    if(strpos($domain_name, '.') === FALSE OR $domain_name[strlen($domain_name)-1]=='.' OR $domain_name[0]=='.')
        return FALSE;

    //now we use the FILTER_VALIDATE_URL, concatenating http so we can use it, and return BOOL
    return (filter_var ('http://' . $domain_name, FILTER_VALIDATE_URL)===FALSE)? FALSE:TRUE;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM