Running the following Code
Returns HTTP 200 instead of 404 For any domain or URL that does not exist
Array (  => HTTP/1.1 200 OK  => Server: nginx/1.1.15  => Date: Mon, 08 Oct 2012 12:29:13 GMT  => Content-Type: text/html; charset=utf-8  => Connection: close  => Set-Cookie: PHPSESSID=3iucojet7bt2peub72rgo0iu21; path=/; HttpOnly  => Expires: Thu, 19 Nov 1981 08:52:00 GMT  => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0  => Pragma: no-cache  => Set-Cookie: bypassStaticCache=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; httponly  => Set-Cookie: bypassStaticCache=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; httponly  => Vary: Accept )
If you Run
Array (  => HTTP/1.1 404 Not Found  => Date: Mon, 08 Oct 2012 12:32:18 GMT  => Content-Type: text/html  => Content-Length: 8727  => Connection: close  => Server: Apache  => Vary: Accept-Encoding )
They are so many instances where
get_headers has been proven to be a solution to validate existing URL
- What is the best way to check if a URL exists in PHP?
- How can I check if a URL exists via PHP?
Is This is a Bug or get_headers is not a reliable way for validating URL
See Live Demo
Got to find out that CURL also has the same issue
$curl = curl_init(); curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => true,CURLOPT_URL => 'idontexist.tld')); curl_exec($curl); $info = curl_getinfo($curl); curl_close($curl); var_dump($info);
Also returns the same result
The problem is nothing to do with the length of the domain name, it is simply whether the domain exists.
You are using a DNS service that resolves non-existent domains to a server that gives you a "friendly" error page, which it returns with a 200 response code. This means it is also not a problem with
get_headers()specifically, it is any procedure with an underlying reliance on sensible DNS lookups.
A way to handle this without hardcoding a work around for every environment you work in might look something like this:
You may want to somehow cache the return value of the first call to
gethostbyname(), since you know you are looking up a name that does not exist, and this can often take a few seconds.