Skip to content Skip to sidebar Skip to footer

How To Check If A Webpage Exists. Jquery And/or Php

I want to be able to validate a form to check if a website/webpage exists. If it returns a 404 error then that definitely shouldn't validate. If there is a redirect...I'm open to s

Solution 1:

It sounds like you don't care about the web page's contents, you just want to see if it exists. Here's how I'd do it in PHP - I can stop PHP from taking up memory with the page's contents.

/*
 * Returns falseif the page could not be retrieved (ie., no 2xx or 3xx HTTP
 * status code). On success, if$includeContents = false (default), then we
 * returntrue - if it's true, then we return file_get_contents()'s result (a
 * string of page content).
 */
function getURL($url, $includeContents = false)
{
  if($includeContents)
    return @file_get_contents($url);

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

For less verbosity, replace the above function's contents with this.

return ($includeContents) ? 
               @file_get_contents($url) :  
               (@file_get_contents($url, null, null, 0, 0) !== false)
;

See http://www.php.net/file_get_contents for details on how to specify HTTP headers using a stream context.

Cheers.

Solution 2:

First you need to check that the page exists via DNS. That's why you say it "just hangs" - it's waiting for the DNS query to time out. It's not actually hung.

After checking DNS, check that you can connect to the server. This is another long timeout if you're not careful.

Finally, perform the HTTP HEAD and check the status code. There are many, many, many special cases you have to consider here: what does a "temporary internal server error" mean for the page existing? What about "permanently moved"? Look into HTTP status codes.

Solution 3:

I've just written a simpler version using PHP:

functionurl_check($url) {

$x = @fopen($url,"r");

if ($x) {

         $reply = 1;

         fclose($x);

} else {

            $reply = 0;

}

return$reply;

}

Obviously $url is the test URL, returns true (1) or false (0) depending on URL existence.

Solution 4:

Maybe you could combine domain checker, and jQuery, domain checker (PHP) can respond 1 or 0 for non-existent domains.

eg. http://webarto.com/snajper.php?domena=stackoverflow.com , will return 1, you can use input blur function to check for it instantly.

Post a Comment for "How To Check If A Webpage Exists. Jquery And/or Php"