Jump to content

Recommended Posts

I'm in a situation where I get the user's desired page via a $_GET variable. The only problem is that, of course, this means that they could quite easily use directory traversal to view pages they shouldn't be able to. What I want is to check that the user's page doesn't contain anything but letters/numbers and a '.' in the middle, e.g. folder.file, but not folder.../file

 

I've been looking at various sites, and I came to ([A-Za-z0-9-]+)\.([A-Za-z0-9-]+)

Is this ok? Also, is there any way of shortening it? I know you can use \w to match words, but these don't contain numbers right?

Thanks in advance

 

----------------

Now playing: Dance Gavin Dance - It's Safe to Say You Dig the Backseat

via FoxyTunes

Link to comment
https://forums.phpfreaks.com/topic/115561-match-wordword/
Share on other sites

Just to clarify, it would be

 

if(preg_match('%\.\./%', $string) { /* Do action if unsafe */ } else { /* Action if safe */ }

?

 

----------------

Now playing: Get Cape. Wear Cape. Fly - The Children Are (The Consumers Of) The Future

via FoxyTunes

Link to comment
https://forums.phpfreaks.com/topic/115561-match-wordword/#findComment-595930
Share on other sites

No underscores, just words. However, these will be in camel case if more than one word is needed, so would I need:

 

[pre]/\A[A-Za-z\d]\.[A-Za-z\d]\z/i[/pre]

And how would I add the %\.\./% in?

 

----------------

Now playing: Get Cape. Wear Cape. Fly - Better Things

via FoxyTunes

Link to comment
https://forums.phpfreaks.com/topic/115561-match-wordword/#findComment-595942
Share on other sites

It's a path, but with dot notation. For example:

 

If 'topFolder.bottomFile' is matched, it will go to topFolder/bottomFile.php. I can work out the .php bit, and the / bit, but it's the validation I'm struggling with :S

 

----------------

Now playing: Get Cape. Wear Cape. Fly - Could've Seen It All

via FoxyTunes

Link to comment
https://forums.phpfreaks.com/topic/115561-match-wordword/#findComment-595949
Share on other sites

Typically ^ and $ are used to anchor the beginning and end of the line, respectively; however these are affected by multi-line mode (/m). The other options are:

 

\A -- Same as ^, but not affected by multi-line mode.

\Z -- Same as $, but not affected by multi-line mode. May match before a string-ending new line.

\z -- Same as $, but not affected by multi-line mode. Will only match the end of the string.

 

Link to comment
https://forums.phpfreaks.com/topic/115561-match-wordword/#findComment-597563
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.