19

Possible Duplicate:
what is the best way to check if a Url exists in PHP ?`

I'm looking for a function that returns TRUE or FALSE in php, either the URL is valid or not.

isValidURL($url); I think that simple... That would take into count all kind of URL possible.

By valid I want it to refer to an existing page of the web or other kind of files. It just should exist.

shaedrich
  • 5,457
  • 3
  • 26
  • 42
james
  • 413
  • 4
  • 7
  • 12
  • 2
    And valid means what? Syntactically valid? Or references an existing resource? – Gumbo Jul 24 '11 at 13:05
  • @Gordon: That depends on James’ definition of validity. – Gumbo Jul 24 '11 at 13:11
  • hey guys thanks a lot for your help. By valid i want It to referes to an existing page of the web or other kind of files. It just should exist... – james Jul 24 '11 at 13:17
  • 1
    possible duplicates of http://stackoverflow.com/search?q=check+if+url+exists+php – Gordon Jul 24 '11 at 13:21
  • A valid url is defined by its RFC http://www.faqs.org/rfcs/rfc1738.html – fyr Jul 24 '11 at 13:23
  • thanks everyone, sorry for the trouble. you still helped me a lot – james Jul 24 '11 at 14:27
  • This many or may not be a duplicate, but it is not a duplicate of the question that it is linked to as a duplicate. This question is asking about "valid" URLs, that question is asking about "available" URLS. Entirely possible that a valid URL is not available. – ftrotter Aug 02 '15 at 23:20

2 Answers2

60
<?php

$url = "http://stack*overflow.org";


if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
{
        echo "Not valid";
}else{
        echo "VALID";
}
?>

this does not check tlds though

genesis
  • 50,477
  • 20
  • 96
  • 125
  • this is good one, but your own example is validates to `true` ;) `http://stackoverflow.invalid`. So it's not reliable too ;-) – Nemoden Jul 24 '11 at 13:26
  • 1
    @Nemoden: I know it isn't. But I told that already in my post... – genesis Jul 24 '11 at 13:37
  • 8
    @Nemoden: What's wrong with the URL stackoverflow.invalid? You can setup your own local DNS to handle all sorts of TLD's. I have *.lan pointing to several local development sites behind a firewall. ;) – Dustin Graham May 20 '13 at 03:12
  • Note: this will also allow `http://stackoverflow` with no extension at all. – PaulSkinner Jun 19 '13 at 11:01
  • 3
    http://stackoverflow is also a valid url, extentions are not required for valid urls, localhost is a valid url too – Shahrokhian Aug 24 '13 at 09:21
  • I guess it's a good idea to use this first and than do more reliable but slower check using `get_headers()`. – Tomáš Zato Jan 19 '14 at 21:00
  • Note: neither `http://åäö.se` or even `http://example.com?s=åäö` is considered valid by this method. – Jonas Äppelgran May 20 '14 at 13:09
  • IPv6 addresses are also considered invalid by this method. – rink.attendant.6 May 20 '14 at 16:18
  • @JonasÄppelgran - Neither `http://åäö.se` nor `http://example.com?s=åäö` are valid URL, though browsers display those characters.Technically, the browser uses punycode to encode domain names and url-encodes parameters. So the correct URLs are `http://xn--4cab6c.se/` and `http://example.com/?s=%C3%A5%C3%A4%C3%B6` – Philipp May 04 '21 at 10:29
2

You can check whether URL is valid or not using parse_url function which would return false if URL is not valid and an array otherwise.

function isValidURL($url) { return (bool)parse_url($url); }

pretty easy way, huh? :)

Nemoden
  • 8,816
  • 6
  • 41
  • 65
  • It isn't reliable:http://sandbox.phpcode.eu/g/a4192.php – genesis Jul 24 '11 at 13:15
  • 2
    Then `preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);` isn't reliable too. But I have my own reason to don't care about domain zone: they will register more domain zones tommorow and I will have to add all of them in all my projects manually? To check if URL is REALLY exists I will use cURL or sockets. For quick check this functions is enough for me. I just propose it to the OP and I don't mind if he will not mark my answer as accepted. I just offer an approach which I use myself. And it's more clean rather than `regex` that's why I like it ;-) – Nemoden Jul 24 '11 at 13:24
  • not reliable? schema is good, you should check if it exists the name with gethostbyname: function isValidURL($url) { $parts = parse_url($url); if((bool)$parts) { return ($parts['host'] != gethostbyname($parts['host']);} else return false; } – Daniele Cruciani Sep 21 '12 at 06:16
  • 2
    The docs explicitly say "This function is not meant to validate the given URL"... – Kloar Nov 22 '12 at 10:19
  • @Kloar, "is not meant" does not mean "it can not". – Nemoden Nov 23 '12 at 01:11
  • yeah, +1 it doesn't really work... – Gabriel Queiroz Silva Apr 01 '16 at 19:36
  • @Nemoden it also can not though, there are absolutely tons of ways to get invalid urls through parse_url – Shardj Aug 07 '20 at 13:30