lisovaccaro
lisovaccaro

Reputation: 33956

How to check if there is anything at 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.

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?

Upvotes: 2

Views: 1009

Answers (4)

danielrsmith
danielrsmith

Reputation: 4060

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

Upvotes: 1

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

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

Upvotes: 1

Ortiga
Ortiga

Reputation: 8814

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

Upvotes: 1

Brad
Brad

Reputation: 163242

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

Upvotes: 4

Related Questions