Jump to content


Photo

Checking if correct url?


  • Please log in to reply
6 replies to this topic

#1 tiki

tiki
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 27 June 2006 - 06:49 AM

This is supposed to check if the website is valid and has http://.
If it does not match it echos an error, but it doesnt seem to work correctly.

<?php
if (!eregi("^((http|https|ftp)://)?([[:alnum:]-])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_.~?-]*)$", stripslashes(trim($_POST["website"])))) {
?>


#2 tiki

tiki
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 05 July 2006 - 10:03 AM

Anyone?

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 05 July 2006 - 01:51 PM

Which part isn't working correctly? Did you get any error messages from PHP? Is it allowing invalid data? Is it allowing valid data?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 05 July 2006 - 02:08 PM

Anyone?


Well, I'm mostly acquainted with Perl's regular expressions, but it looks like you're trying to match a possible URI followed by some alphanumeric characters (or dashes) followed by a dot followed by 2-4 more alphanumeric characters, then any number of alphanumeric characters plus plenty of symbols, am I right?

So something like "http://phpfreaks.com/" would match while "www.phpfreaks.com" would not.  Nor would an IP address, "http://66.97.171.5/f...rums/index.php"

I don't think your regular expression is very robust in terms of encompassing all possible valid forms of website addresses.

You might also consider using one of PHP's filesystem functions or network functions to attempt to access the page the user has specified in order to confirm its existence.  I don't know if that would work all the time, but it's a thought.
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#5 tiki

tiki
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 14 July 2006 - 09:10 PM

It allows it through when it does not have a http:// on the front of it, shouldnt this make it so it doesnt pass unless its there?

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 14 July 2006 - 09:13 PM

Change ^((http|https|ftp)://)? to ^(https?|ftp)://
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#7 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 14 July 2006 - 09:14 PM

your pattern uses the ? quantifier after the optional http|https|ftp set.  '?' specifies EITHER 0 or 1 instances of the sub-pattern, so a string with 0 instances of that sub-pattern will be allowed through.

you'll want the 1 or more quantifier, which i can't remember off the top of my head.  have a look in the php manual at syntax.

EDIT:  or do what effigy said.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users