1

I need to determine if a URL exits. I ran across this post.

How can I check if a URL exists via PHP?

$file = 'http://godaddy';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') 
{
    $exists = false;
}
else 
{
    $exists = true;
}

And implemented this code and when I tested it as a user who forgot to put in the .com it comes back with true. Which isn't correct because if you go to http://godaddy there is no website.

I tried validating the $file before hand but

filter_var($url, FILTER_VALIDATE_URL);

views http://godaddy as a valid url.

Any idea how to handle this sort of input?

var_dump($file_headers)= array(8) {
    [0]=> string(15) "HTTP/1.1 200 OK"
    [1]=> string(13) "Server: nginx"
    [2]=> string(35) "Date: Mon, 29 Jun 2015 14:23:07 GMT"
    [3]=> string(23) "Content-Type: text/html"
    [4]=> string(17) "Connection: close"
    [5]=> string(21) "Vary: Accept-Encoding"
    [6]=> string(38) "Expires: Mon, 29 Jun 2015 14:23:06 GMT"
    [7]=> string(23) "Cache-Control: no-cache"
}
Community
  • 1
  • 1
tomjung
  • 715
  • 3
  • 11
  • 34
  • 2
    `var_dump($file_headers)`. you're not going to get an http error code is an http request couldn't be performed in the first place. `http://godaddy` WOULD be a valid url **IF** you had a host named `godaddy` in whatever your local DNS zone is. – Marc B Jun 29 '15 at 14:19
  • 1
    Your DNS host (ISP or such) may be presenting you with a default error/search page for non-existing domains. – deceze Jun 29 '15 at 14:39

1 Answers1

0

Try without the @ character. in this way you can see the direct error. I think the error masking cannot permit the correct reading of the response of the get_header

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107