Easy and efficient way to find if a URL exists in PHP

by barkkathulla 2013-03-01 09:20:13

To find if an URL exists in PHP or not



This method is the easiest way to determine the specified url is having or not in a PHP file...

function urlOK($url)
{
$url_data = parse_url ($url);
if (!$url_data) return FALSE;

$errno="";
$errstr="";
$fp=0;

$fp=fsockopen($url_data['host'],80,$errno,$errstr,30);

if($fp===0) return FALSE;
$path ='';
if (isset( $url_data['path'])) $path .= $url_data['path'];
if (isset( $url_data['query'])) $path .= '?' .$url_data['query'];

$out="GET /$path HTTP/1.1rn";
$out.="Host: {$url_data['host']}rn";
$out.="Connection: Closernrn";

fwrite($fp,$out);
$content=fgets($fp);
$code=trim(substr($content,9,4));
fclose($fp);
return ($code[0] == 2 || $code[0] == 3) ? TRUE : FALSE;
}
if (urlOK($url)) echo " is a working URL";
else echo " is a bad URL";
?>

From the above code we can get the information like if the URL exists in a particular PHP file.
954
like
0
dislike
0
mail
flag

You must LOGIN to add comments