I have a PHP function which is used to check if a URL exists. It works very well on my old web server. However, when I switched to my new hosting providers, It can’t work any more. The code of the function is as following:
< ?php
function check_url($url)
{
$furl = @fopen($url, "r");
if (!$furl) {
return false;
}
@fclose($furl);
return true;
}
?>
Aftering the PHP manual, I found the following section:
If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.
When I looked at the phpinfo() page. I found that the parameter “allow_url_fopen” was set to “Off”. Although someone recommend setting this parameter to On using ini_set at the first line of the script:
< ?php
ini_set("allow_url_fopen", "1");
?>
This made no difference. In the PHP manual, I found the root cause of the problem.
allow_url_fopen boolean
This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
Note: This setting can only be set in php.ini due to security reasons.
Note: This option was introduced immediately after the release of version 4.0.3. For versions up to and including 4.0.3 you can only disable this feature at compile time by using the configuration switch –disable-url-fopen-wrapper.
Finally, I implemented my function using CURL which can be used to connect and communicate to many different types of servers with many different types of protocols.
< ?php
function check_url($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
curl_exec ($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
if ($code != 200 && $code != 302 && $code != 304) {
return false;
} else {
return true;
}
}
?>
Or
< ?php
function check_url($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/", $data, $matches);
$code = end($matches[1]);
if (!$data) {
return 0; // "Domain could not be found";
} else {
if ($code == 200) {
return true; // "Page Found";
} elseif ($code == 404) {
return false; // "Page Not Found";
}
}
}
?>
And the following is the usage example:
< ?php
if (check_url("http://www.memezilla.com")) {
echo "URL is OK!";
} else {
echo "URL Error";
}
?>