Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, validate that it is a valid URL using <a href="http://php.net/filter_var" rel="nofollow noreferrer"><code>filter_var()</code></a>:</p> <pre><code>&lt;?php $url = "http://www.example.com/foo.php"; if(!filter_var($url, FILTER_VALIDATE_URL)) { die('Invalid URL'); } </code></pre> <p>Next, parse the URL with <a href="http://php.net/parse_url" rel="nofollow noreferrer"><code>parse_url()</code></a> and ensure that it is HTTP(S):</p> <pre><code>$p_url = parse_url($url); if(!$p_url) // couldn't parse URL, since parse_url() cannot recognize it { die('Invalid URL'); } if($p_url['scheme'] != 'http' &amp;&amp; $p_url['scheme'] != 'https') { die('Invalid protocol (only HTTP(S) is supported)'); } </code></pre> <p>Lastly, check that the host exists and that you can connect to it. I choose to use <a href="http://php.net/fsockopen" rel="nofollow noreferrer"><code>fsockopen()</code></a> here, since it would check the hostname and port, while not actually sending an HTTP request.</p> <pre><code>$fp = fsockopen($p_url['host'], (isset($p_url['port']) ? $p_url['port'] : 80)); if($fp) { echo 'Valid URL'; fclose($fp); // Remember to close the file pointer }else{ echo 'Invalid server'; } </code></pre> <p>Note that you might want to avoid using this method (depending on what you want your application to do), as if the server is down, this would result in <code>Invalid server</code>, even though the server might exist. An alternative solution that will only check the hostname from the DNS servers, and not connect at all, is using <a href="http://php.net/gethostbyaddr" rel="nofollow noreferrer"><code>gethostbyaddr()</code></a> and <a href="http://php.net/gethostbyname" rel="nofollow noreferrer"><code>gethostbyname()</code></a>:</p> <pre><code>if(@gethostbyaddr($p_url['host'])) // IP addresses passes this test { echo 'Valid URL'; }else{ $host = $p_url['host']; $addr = gethostbyname($host); if(!$addr) // Invalid domain name { echo 'Invalid domain name'; }else if($host == $addr) // Domain name could not be resolved (i.e. does not exist) { echo 'Invalid domain name'; }else{ echo 'Valid URL'; } } </code></pre> <p>Links:</p> <ul> <li><a href="http://php.net/filter_var" rel="nofollow noreferrer"><code>filter_var()</code></a> - Validate strings</li> <li><a href="http://php.net/parse_url" rel="nofollow noreferrer"><code>parse_url()</code></a> - Parses URLs into arrays</li> <li><a href="http://php.net/fsockopen" rel="nofollow noreferrer"><code>fsockopen()</code></a> - Opens a socket connection</li> <li><a href="http://php.net/gethostbyaddr" rel="nofollow noreferrer"><code>gethostbyaddr()</code></a> - Gets the domain name of an IP address (not sure if it supports IPv6, though)</li> <li><a href="http://php.net/gethostbyname" rel="nofollow noreferrer"><code>gethostbyname()</code></a> - Gets the IPv4 address of a domain name</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload