How to check if a webpage exists. jQuery and / or PHP

I want to be able to check the form to check if a website / webpage exists. If it returns a 404 error, then this should definitely not be checked. If there is a redirect ... I am open to suggestions, sometimes redirects go to the error page or to the main page, sometimes they go to the page you were looking for, so I don’t know. Perhaps there may be a special notification for redirection that suggests the destination address for the user.

The best I've found so far has been as follows:

$.ajax({url: webpage ,type:'HEAD',error:function(){ alert('No go.'); }}); 

This is not a problem with 404 and 200, but if you do something like 'http://xyz' for the url, it just freezes. Also 302, etc. start the error handler.

This is a fairly general question that I would like to get if someone could create it. It can be convenient for many people.

+6
javascript url php validation
source share
4 answers

It sounds like you don't care about the content of the webpage, you just want to see if it exists. Here's how I will do it in PHP - I can stop PHP from capturing memory with the contents of the page.

 /* * Returns false if the page could not be retrieved (ie., no 2xx or 3xx HTTP * status code). On success, if $includeContents = false (default), then we * return true - if it true, then we return file_get_contents() 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 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 more information on how to specify HTTP headers using a stream context.

Greetings.

+3
source share

First you need to check if the page exists through DNS. That's why you say that it “just hangs” - it waits for the DNS query to expire. In fact, he did not hang.

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

Finally, execute HTTP HEAD and check the status code. There are many, many, many special cases that you should consider here: what does “temporary internal server error” mean for an existing page? How about being "constantly displaced"? View HTTP status codes.

+1
source share

I just wrote a simpler version using PHP:

 function url_check($url) { $x = @fopen($url,"r"); if ($x) { $reply = 1; fclose($x); } else { $reply = 0; } return $reply; } 

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

+1
source share

Perhaps you could combine domain validation, and jQuery's domain checker (PHP) might respond 1 or 0 to non-existent domains.

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

0
source share

All Articles