is URL valid or not [duplicate]
Asked Answered
K

2

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.

Knott answered 24/7, 2011 at 13:0 Comment(7)
And valid means what? Syntactically valid? Or references an existing resource?Cheongsam
@Gordon: That depends on James’ definition of validity.Cheongsam
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...Knott
possible duplicates of stackoverflow.com/search?q=check+if+url+exists+phpConclude
A valid url is defined by its RFC faqs.org/rfcs/rfc1738.htmlTorment
thanks everyone, sorry for the trouble. you still helped me a lotKnott
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.Repair
A
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

Arliearliene answered 24/7, 2011 at 13:19 Comment(9)
this is good one, but your own example is validates to true ;) http://stackoverflow.invalid. So it's not reliable too ;-)Hypno
@Nemoden: I know it isn't. But I told that already in my post...Arliearliene
@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. ;)Tenaille
Note: this will also allow http://stackoverflow with no extension at all.Carlita
stackoverflow is also a valid url, extentions are not required for valid urls, localhost is a valid url tooIdocrase
I guess it's a good idea to use this first and than do more reliable but slower check using get_headers().Crummy
Note: neither http://åäö.se or even http://example.com?s=åäö is considered valid by this method.Natashianatassia
IPv6 addresses are also considered invalid by this method.Nalchik
@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%B6Ditch
H
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? :)

Hypno answered 24/7, 2011 at 13:6 Comment(7)
It isn't reliable:sandbox.phpcode.eu/g/a4192.phpArliearliene
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 ;-)Hypno
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; }Philippa
The docs explicitly say "This function is not meant to validate the given URL"...Misfortune
@Kloar, "is not meant" does not mean "it can not".Hypno
yeah, +1 it doesn't really work...Biannulate
@Hypno it also can not though, there are absolutely tons of ways to get invalid urls through parse_urlYell

© 2022 - 2024 — McMap. All rights reserved.