简体   繁体   中英

Checking for existence of URL in PHP

If I display a URL link in PHP, is there any way to check for validity after the user click so as to display the user a nice custom message that the URL is broken or something like that?

I do not mean an error 404 page. I guess error 404 is only for internal website pages but not external links. Please correct me if I am wrong.

My suggestion: Write a PHP batch job that regularly checks URLs (with curl or fsockopen) and marks them in your data. This way, you know that the URL is broken before you display it to the user.

The short answer is — no.

Clicking a link causes the user's browser to request the resource from the server it is hosted on. Your server is not involved.

You could write some JavaScript that cancels the normal behavior of the link, uses Ajax to make a request to your server, have PHP on your server make a request to the third party site to check the response, respond to the Ajax request, and then set location to either the original URL or one for your error message … but that would be a significant slow down in performance for the user.

If you are worried about links you provide being broken, periodically check them. You could automate this (eg with checklink )

You should use fsockopen to find the response code of the link first then display it if it is less than 400. See http://www.scriptol.com/how-to/http-status-code-in-php.php . Perhaps the best idea would be to display a warning next to broken links (easily doable via CSS or javascript).

404 is for any resource not found. If a link points to one, it is the task of the server where the resource does not exist to give you this message. Quoting the RFC for HTTP Status Codes :

10.4.5 404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

In addition, clicking a link in the browser takes place long after PHP is done with the Page. PHP is server side, while user interaction happens completely isolated from this on the user's computer. So, there is no way to capture the click with PHP.

If you would want to capture clicks (technically it isn't), you'd have to route all external links to your own webserver that could prefetch the link via get_headers() to see if the resource exists. If not, you could present your own custom page to the user. But keep in mind that this is actually doing two HTTP requests then. First from user to your server, then from your server to the external page and your external links would probably look like this:

http://www.yourserver.com/external.php?url=example.com/someuri

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM