-1

Possible Duplicate:
How can one check to see if a remote file exists using PHP?
How to check if a webpage exists. jQuery and/or PHP

How can I check if a page from a different domain exists using php or javascript?

I've tried this:

function getURL($url, $includeContents = false)
{
  if($includeContents)
    return @file_get_contents($url);

  return (@file_get_contents($url, null, null, 0, 0) !== false);
}


$test = getURL("http://www.google.com");

echo "<script language='javascript'>alert('$test');</script>";

but it doesn't work.

If I use a local url it works.


How to solve this?

Community
  • 1
  • 1
aF.
  • 64,980
  • 43
  • 135
  • 198

2 Answers2

2

it can not be done by pure Javascript.

In PHP, request the page and check if it has 200 http response code. This result can be returned to JS via ajax call so that JavaScript can show a message or do other action accordingly.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
2

To read content from other pages i`m using CURL and do it like that

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$page_content = curl_exec($ch);   
curl_close($ch); 
if ($page_content != "false") {
 // do something with content  
} else {
  // this page is not available
}
Anton Sementsov
  • 1,196
  • 6
  • 18
  • 34
  • Or just check the headers like in the mentioned dupe: http://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php `$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);` – Treffynnon Feb 27 '12 at 15:30