Finding out whether a website returns 200

116 Views Asked by At

I was using the following code to check whether a link that a user provides really points to an existing website:

$headers=get_headers($imageurl);
if (strpos($headers[0], '200') === false) {
    echo "not valid1";
    exit;
}

As test, I use the img http://cdn.thegloss.com/files/2012/04/stormtr.jpg

As long as I use this on my localhost, it works fine - i.e. it said that the url is valid and did not echo "not valid1" - now it is on a different server and it echoes "not valid1". How come? Anybody any idea?

Thanks! Dennis

2

There are 2 best solutions below

1
On BEST ANSWER

get_headers() returns false because it failed.
It might be because allow_url_fopen is set to Off on the server.

1
On

Try this it will return only the 3-digit HTTP response code

function get_response_code($theURL) {
    $headers = get_headers($theURL);
    return substr($headers[0], 9, 3);
}