Does a particular URL exist?

A difficult question and one there isn't a general answer to - but you can check whether an http:// URL is readable which is often the question that's being asked. With a URL such as a mailto:, the only way to check whether it exists is to send an email to it and see if you get an answer back from the address owner. You also need to take care with testing like this to ensure that the user can't enter local file URLs on your server and use it to check the existance (or otherwise) of files / directories ....

Checking URL http://www.wellho.net/net/new.html
This URL s readable

Please enter a URL you would like to validate (existance check):
The URL: and

Note - in order to save myself the need to monitor the use of this script, I have only loaded it here it its tested (HTML) form and the form just above will not work. You are welcome AT YOUR OWN RISK to test the code that's presented below on your own server. Unless you are very sure of what you are doing, I advise you NOT to put this script onto a live Internet server
Copyright Well House Consultants 2005

Source code of this example:

<?php

if ($_REQUEST[url] != "") {
    
$result = 1;
    if (!
ereg("^https?://",$_REQUEST[url])) {
        
$status = "This demo requires a fully qualified http:// URL";
    } else {
        if (@
fopen($_REQUEST[url],"r")) {
        
$status = "This URL s readable";
        } else {
            
$status = "This URL is not readable";
        }
    }
} else {
    
$result = 0;
    
$status = "no URL entered (yet)";
}

?>
<html><head><title>Does a URL exist?</title></head>
<body bgcolor="white"><table width="600"><tr><td>
<h1>Does a particular URL exist?</h1>
A difficult question and one there isn't a general answer to - but
you can check whether an http:// URL is readable which is often the
question that's being asked. With a URL such as a mailto:, the only
way to check whether it exists is to send an email to it and see if
you get an answer back from the address owner.   You also need to
take care with testing like this to ensure that the user can't
enter local file URLs on your server and use it to check the
existance (or otherwise) of files / directories .... <br /><br />
<?php
if ($result != 0) {
   print
"Checking URL <b>".htmlspecialchars($_REQUEST[url])."</b><br />";
}
print
"$status";
?>
<br />
<hr />
Please enter a URL you would like to validate (existance check):
<form>The URL: <input name="url" /> and <input type="submit" /></form>
<hr />
Copyright <a href="http://www.wellho.net/">Well House Consultants</a>
<?= date("Y") ?><br /><br />
<b>Source code of this example:</b><br /><br />
<?= highlight_file($_SERVER[SCRIPT_FILENAME],1) ?>
</td></tr></table>
</body></html>
    


Training sample © 2024, WELL HOUSE CONSULTANTS LTD
This is http://www.wellho.net/demo/urlex.html
See source code here • More examples - same topic here
Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
Phone: +44 (0) 1225 708 225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net