Checking if URL is valid

This is a discussion on Checking if URL is valid within the PHP Language forums, part of the PHP Programming Forums category; What kind of regular expression pattern is needed to check if URL is valid? It's enought if most of ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-20-2005
John V
 
Posts: n/a
Default Checking if URL is valid

What kind of regular expression pattern is needed to check
if URL is valid? It's enought if most of cases are covered.

I have PHP 4.x.

Br
Reply With Quote
  #2 (permalink)  
Old 02-20-2005
Randell D.
 
Posts: n/a
Default Re: Checking if URL is valid

John V wrote:

> What kind of regular expression pattern is needed to check
> if URL is valid? It's enought if most of cases are covered.
>
> I have PHP 4.x.
>
> Br


Personally, I check if the last part of the domain name (the .com, .net,
..info) part is greater than one character (to allow for country specific
domain names like ca or uk) and less than 8 characters (because I
believe there are also .museum or similar available on the net, andmore
will eventually follow).

I should (but don't) check if the last part of the domain name is
numeric (thus instead of .com that the user had entered .co1 or something).

I do check that there are at least three 'portions' to the domain name -
thus http://www.com would fail but www.xyz.com would pass - Or xyz.co.uk
would also pass...

And what about the protocol? Are you only refering to web http://
style addresses? If true, then check for that too... I believe there is
a maximum length for a domain name but I don't know what that is - I
have a self imposed limit of 64characters.

Lastly - your url should I believe only contain alphanumeric input, in
addition to _ (underscore) characters (though I believe you could also
have a dash, but I think this is not recommended).

Does that help you any?
randelld

Reply With Quote
  #3 (permalink)  
Old 02-20-2005
Daniel Tryba
 
Posts: n/a
Default Re: Checking if URL is valid

Randell D. <reply.via.news.group.only.thanks@fiprojects.moc > wrote:
>> What kind of regular expression pattern is needed to check
>> if URL is valid? It's enought if most of cases are covered.


You can't check it this way. Why not check if it actually exists?

> Personally, I check if the last part of the domain name (the .com, .net,
> .info) part is greater than one character (to allow for country specific
> domain names like ca or uk) and less than 8 characters (because I
> believe there are also .museum or similar available on the net, andmore
> will eventually follow).


foo@bar.invalid is a valid URL accoring to RFC 2606 but email will never
be delivered to it.

> I do check that there are at least three 'portions' to the domain name -
> thus http://www.com would fail but www.xyz.com would pass - Or xyz.co.uk
> would also pass...


Funny example:

lynx -head -dump http://www.com/

HTTP/1.0 200 OK
Server: Resin/2.1.14
ETag: "AAAAQH7CT4Q"
Last-Modified: Thu, 10 Feb 2005 06:51:22 GMT
Content-Type: text/html
Content-Length: 9538
Date: Sun, 20 Feb 2005 03:23:06 GMT

A valid URL it seems

> And what about the protocol? Are you only refering to web http://
> style addresses? If true, then check for that too... I believe there is
> a maximum length for a domain name but I don't know what that is - I
> have a self imposed limit of 64characters.
>
> Lastly - your url should I believe only contain alphanumeric input, in
> addition to _ (underscore) characters (though I believe you could also
> have a dash, but I think this is not recommended).


Heard of IDN?

So while above test might be a good start (and be better than no checks)
the best test is to actually check if the URL is valid. Even better
would be a kind of challenge/response test (a must for email URLs).

Reply With Quote
  #4 (permalink)  
Old 02-20-2005
Paul Herber
 
Posts: n/a
Default Re: Checking if URL is valid

On Sun, 20 Feb 2005 00:56:31 GMT, "Randell D."
<reply.via.news.group.only.thanks@fiprojects.moc > wrote:

>I do check that there are at least three 'portions' to the domain name -
>thus http://www.com would fail but www.xyz.com would pass - Or xyz.co.uk
>would also pass...


http://xyz.com is a valid URL.

--
Regards, Paul Herber, Sandrila Ltd. http://www.pherber.com/
SanDriLa - SDL/MSC/TTCN/UML2 application for Visio http://www.sandrila.co.uk/
email address is spam-trapped - s/*@/paul@/
Reply With Quote
  #5 (permalink)  
Old 02-20-2005
Markku Uttula
 
Posts: n/a
Default Re: Checking if URL is valid

Daniel Tryba wrote:
> foo@bar.invalid is a valid URL accoring to RFC 2606 but email will
> never be delivered to it.


Funny - I was under the impression that email is never delivered to
URLs :)

--
Markku Uttula

Reply With Quote
  #6 (permalink)  
Old 02-20-2005
Markku Uttula
 
Posts: n/a
Default Re: Checking if URL is valid

Randell D. wrote:
> Personally, I check if the last part of the domain name (the .com,
> .net, .info) part is greater than one character (to allow for
> country
> specific domain names like ca or uk) and less than 8 characters
> (because I believe there are also .museum or similar available on
> the net,
> andmore will eventually follow).


And thus you negate the possibility of passing in numeric URLs. Never
heard of IP names?

> I do check that there are at least three 'portions' to the domain
> name - thus http://www.com would fail but www.xyz.com would pass -
> Or
> xyz.co.uk would also pass...


Why? As has been pointed out, www.com exists and is fully valid URL.
There are others too.

> And what about the protocol? Are you only refering to web http://
> style addresses? If true, then check for that too... I believe
> there
> is a maximum length for a domain name but I don't know what that
> is - I
> have a self imposed limit of 64characters.


What matter does that have? The question was about URL, and in that
domain name length plays a very little part.

> Lastly - your url should I believe only contain alphanumeric input,
> in
> addition to _ (underscore) characters (though I believe you could
> also
> have a dash, but I think this is not recommended).


In addition, there can be a lot more characters, because we're talking
about a URL, not "domain name part" of URL.

--
Markku Uttula

Reply With Quote
  #7 (permalink)  
Old 02-20-2005
Markku Uttula
 
Posts: n/a
Default Re: Checking if URL is valid

John V wrote:
> What kind of regular expression pattern is needed to check
> if URL is valid? It's enought if most of cases are covered.


I wouldn't recommend using regular expression to do that. Why do it
the hard way :)

A much better solution is to attempt to connect to the given domain,
and if that is possible, request (for example) HEAD-command with the
URL entered. This would (of course) only check if the connection to
the given URL is possible at the given time from the machine you're
running the script from, but this is always the case with WWW :)

--
Markku Uttula

Reply With Quote
  #8 (permalink)  
Old 02-20-2005
Daniel Tryba
 
Posts: n/a
Default Re: Checking if URL is valid

Markku Uttula <markku.uttula@disconova.com> wrote:
>> foo@bar.invalid is a valid URL accoring to RFC 2606 but email will
>> never be delivered to it.

>
> Funny - I was under the impression that email is never delivered to
> URLs :)


A common mistake, worth reading:

mailto URL scheme:
http://www.ietf.org/rfc/rfc2368.txt
Generic URI syntax:
http://www.ietf.org/rfc/rfc2396.txt

But I see I forgot the scheme from my example url :)
Reply With Quote
  #9 (permalink)  
Old 02-21-2005
Randell D.
 
Posts: n/a
Default Re: Checking if URL is valid

Randell D. wrote:
> John V wrote:
>

[snip]
>> It's enought if most of cases are covered.

[snip]

I never said my solution covered everything - nor did the OP request
this... but I do think my suggestion covered most cases...

Thanks for pointing out things I hadn't thought off though,

randelld
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:38 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0