Blog : Check if url is exist or not
Check if url is exist or not
I've website where visitor would submit links so i would like of the posted url is exist or not so i've been using the following function
function url_exist($url){
$c=curl_init();
curl_setopt($c,CURLOPT_URL,$url);
curl_setopt($c,CURLOPT_HEADER,1);
curl_setopt($c,CURLOPT_NOBODY,1);
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
curl_setopt($c,CURLOPT_FRESH_CONNECT,1);
if(!curl_exec($c)){
return false;
}else{
return true;
}}
$ur = "http://www.google.com"; // example
if(url_exist($url)){
echo "yea valid";
}else{
echo "not valid";
}
?>
but my website become so slow and something not even loading at the step of checking if valid or not valid url so i wonder if there any idea else can do the same without loading too much on my hosting server as the above function !! ~ any help
See this url:-
If URL Exists
http://www.daniweb.com/web-development/php/threads/83076/if-url-exists
and also see this
http://www.namepros.com/code/315565-check-if-a-url-exists.html
Using this code may work
$headers = @get_headers($url);
if(strpos($headers[0],'200')===false)return false;
So if a url returns anything other than code 200, you will know that url is wrong.
$url = 'http://google.com/';
if( get_headers($url) )
echo "$url exists";