Jump to content

Archived

This topic is now archived and is closed to further replies.

tiki

Checking if correct url?

Recommended Posts

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.

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

Share this post


Link to post
Share on other sites
Which part isn't working correctly? Did you get any error messages from PHP? Is it allowing invalid data? Is it allowing [i]valid[/i] data?

Share this post


Link to post
Share on other sites
[quote author=tiki link=topic=96937.msg391830#msg391830 date=1152093830]
Anyone?
[/quote]

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/forums/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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Change ^((http|https|ftp)://)? to ^(https?|ftp)://

Share this post


Link to post
Share on other sites
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.

[b]EDIT:  or do what effigy said.[/b]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.