Checking if URL is valid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John V

    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
  • Randell D.

    #2
    Re: Checking if URL is valid

    John V wrote:
    [color=blue]
    > 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[/color]

    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

    Comment

    • Daniel Tryba

      #3
      Re: Checking if URL is valid

      Randell D. <reply.via.news .group.only.tha nks@fiprojects. moc> wrote:[color=blue][color=green]
      >> What kind of regular expression pattern is needed to check
      >> if URL is valid? It's enought if most of cases are covered.[/color][/color]

      You can't check it this way. Why not check if it actually exists?
      [color=blue]
      > 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).[/color]

      foo@bar.invalid is a valid URL accoring to RFC 2606 but email will never
      be delivered to it.
      [color=blue]
      > 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...[/color]

      Funny example:

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

      HTTP/1.0 200 OK
      Server: Resin/2.1.14
      ETag: "AAAAQH7CT4 Q"
      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
      [color=blue]
      > 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).[/color]

      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).

      Comment

      • Paul Herber

        #4
        Re: Checking if URL is valid

        On Sun, 20 Feb 2005 00:56:31 GMT, "Randell D."
        <reply.via.news .group.only.tha nks@fiprojects. moc> wrote:
        [color=blue]
        >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...[/color]

        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@/

        Comment

        • Markku Uttula

          #5
          Re: Checking if URL is valid

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

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

          --
          Markku Uttula

          Comment

          • Markku Uttula

            #6
            Re: Checking if URL is valid

            Randell D. wrote:[color=blue]
            > 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).[/color]

            And thus you negate the possibility of passing in numeric URLs. Never
            heard of IP names?
            [color=blue]
            > 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...[/color]

            Why? As has been pointed out, www.com exists and is fully valid URL.
            There are others too.
            [color=blue]
            > 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.[/color]

            What matter does that have? The question was about URL, and in that
            domain name length plays a very little part.
            [color=blue]
            > 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).[/color]

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

            --
            Markku Uttula

            Comment

            • Markku Uttula

              #7
              Re: Checking if URL is valid

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

              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

              Comment

              • Daniel Tryba

                #8
                Re: Checking if URL is valid

                Markku Uttula <markku.uttula@ disconova.com> wrote:[color=blue][color=green]
                >> foo@bar.invalid is a valid URL accoring to RFC 2606 but email will
                >> never be delivered to it.[/color]
                >
                > Funny - I was under the impression that email is never delivered to
                > URLs :)[/color]

                A common mistake, worth reading:

                mailto URL scheme:

                Generic URI syntax:


                But I see I forgot the scheme from my example url :)

                Comment

                • Randell D.

                  #9
                  Re: Checking if URL is valid

                  Randell D. wrote:[color=blue]
                  > John V wrote:
                  >[/color]
                  [snip][color=blue][color=green]
                  >> It's enought if most of cases are covered.[/color][/color]
                  [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

                  Comment

                  Working...