Jump to content

check my syntax - security and performance


ixcese

Recommended Posts

hi ,

 

i have this regex which check for valid URL = web address

"/^(http|https|ftp):\/\/(www\.)?([A-Z0-9][A-Z0-9_-]*\.)?[A-Z0-9][A-Z0-9_-]*\.[A-Z]{2,3}(\.[A-Z]{2,3}){0,1}(\/){0,1}(.+)?$/i"

my question is , is this regex good enough?

what about the performance?

and the security , is this regex doing good for it's goal? checking for valid url..

 

thank you..

I like the fact that it matches

ftp://A.US.A879__*@@&&=+=+()<script="javascript_hackme"||DROP_MY_TABLE

 

That's highly original for a "web address" checker. ;)

 

I'd suggest starting over and using something off the shelf.

Let's see, taking something from the RB library, how about this?

 

\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]

 

Not saying that's the perfect regex, but perhaps a better place to start.

 

Wishing you a fun week

Hi ixcese,

 

do you think the expression I gave you from the RB library might work for you?

Or do you have special requirements?

it's quiet a large project and i don't want to mess up this one..

i was looking for something "heavy" in some way , yeah you can laugh :)

I've tried the code you gave me but it gives me errors..

-Code:

$url = 'http//google.com/';
if(preg_match("\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]", $url)){
	echo '1';
}

 

-Error:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

 

 

Yes, I gave you the pure regex.

To use it in PHP, we just need to wrap it in delimiters.

As a working starting point from which we can refine the expression to what you want, try this:

 

$url = 'http://google.com/';
if(preg_match("~^http://[a-z0-9-]+\.[a-z]{2,5}/?$~i", $url)){
	echo '1';
}

This is borrowing from [url=http://www.phpfreaks.com/forums/index.php?topic=353226.0]another thread from today[/url].

 

(The delimiter in this pattern is !.)

 

(The delimiter in this pattern is !.)

 

Sorry, I meant ~.

 

Next, you can add an optional s: https? instead of http.

You can allow ftp if you like: (?:https?|ftp) instead of https?

You can add an optional www.: (?:www\.)?

You can add optional DOT-subdomains: (?:\.[a-z0-9-]+)*

You can add characters to the classes.

Etc.

Archived

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

×
×
  • Create New...

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.