如何检查URL上是否有任何内容?

Part of my site requires user to input URLs, but in case they type the URL incorrectly or just input a non-existent one on purpose I end up with a bad record on my database.

  • E.G in Chrome if there isn't anything at a URL you get the error message "Oops! Google Chrome could not find fdsafadsfadsf.com". (this is the case I'm referring)

This could be solved by checking the URL to see if there is anything, I can only think of one which is loading the external URL in a PHP file and then parsing it's content. But I hope there is a method that doesn't put unneeded strain on my server.

What other ways exist to check if there is anything at a particular URL?

You can use php get_headers($url), which will return false in case there isn't an answer

I would just make a HEAD request. This will work with most servers, and avoids downloading the entire page, so it is very efficient.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

All you have to do is parse the status code returned. If it is 200, then you're good.

Example implementation with cURL here: http://icfun.blogspot.com/2008/07/php-get-server-response-header-by.html

if you're willing to include a tiny Flash embed you can do a crossdomain AJAX call from the client to see if anything useful is at the destination. This would alleviate any Server involvement at all.

http://jimbojw.com/wiki/index.php?title=Introduction_to_Cross-Domain_Ajax

I would use cURL to do this, that way you can specify a timeout on it.

See the comments on: http://php.net/manual/en/function.get-headers.php