Webmaster Forum


Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 05-10-2006, 10:02 AM
ATLien's Avatar
Contributing Member
 
Join Date: 10-13-03
Location: Atlanta, GA
Posts: 1,135
iTrader: 0 / 0%
Regular Expressions Help

Ok, I have tryed for years to figure out how reg expressions work, but I can't figure them out. Can I ask someone here who knows how to right them for PHP to please right me one.

I need it to list all letters, all numbers, underscore, period, dash, (, ), !, ?, and a comma. I pretty much want to check to make sure members are not inputting html or code that could be used for hacking.

Thank, if there is a better way to do this let me know.
 
Reply With Quote
  #2 (permalink)  
Old 05-10-2006, 03:06 PM
themole's Avatar
Junior Member
 
Join Date: 03-28-06
Posts: 23
iTrader: 0 / 0%
You can use strip_tags() to remove tags. I've heard there's some bugs with that so I also use,

$string = str_replace('<', '&lt;', $string);

This will remove any of your non-desired characters:

$string = preg_replace("/[^a-zA-Z0-9._ \(\)!?,]/", "", $string);

-the mole
 
Reply With Quote
  #3 (permalink)  
Old 05-10-2006, 04:19 PM
digiweb's Avatar
Contributing Member
 
Join Date: 04-07-06
Location: Manchester, NH
Posts: 722
iTrader: 1 / 100%
Quote:
list all letters, all numbers, underscore, period, dash, (, ), !, ?, and a comma. I pretty much want to check to make sure members are not inputting html or code that could be used for hacking.
I believe in a white list philosophy of input cleansing. A black list would try to know every possible character that could be input and exclude it. That's hard and not extensible. A white list says, this is it, the defined set, go no further.

If these are the characters:
a-zA-Z0-9_.-()!?,
then there's nothing I can see that would allow hacking.

The two hacking issues to worry about are cross site scripting and sql injection. Cross site scripting requires you to fool the processor into thinking it's getting javascript. You could do that with
PHP Code:
<javascript
or the ascii equivalents or the html entity equivalents -- all of these are excluded from your white list.

The sql injection requires you fool the input into looking like a valid sql statement. I could be totally off on this one so don't bet the farm, but I've never seen that done without allowing the semi-colon, which you're not allowing.


For syntax go with those better than me at regex in php. ;-)
 
Reply With Quote
  #4 (permalink)  
Old 05-10-2006, 04:29 PM
Izzmo's Avatar
v7n Mentor
Latest Blog:
None

 
Join Date: 11-01-03
Location: Kansas
Posts: 1,356
iTrader: 1 / 100%
What you could do, instead of thinking what characters to block, are characters that are allowed. Only set certain characters to be processed, and then you probably won't have problems with the things you talk about above. Of course, there are always ways around this kinda stuff on the Internet, but it's a start.
__________________
Izzmo
Coding Guru Extraordinaire
ZeroWeb Hosting & Design - Customizable hosting for every type of user!
 
Reply With Quote
  #5 (permalink)  
Old 05-10-2006, 04:47 PM
themole's Avatar
Junior Member
 
Join Date: 03-28-06
Posts: 23
iTrader: 0 / 0%
I was at work when I posted earlier (uh-oh!) so I'll clarify things a little bit more now.


It sounds like you just want to remove any javascript or html tags from a user's input. I use the following:

$user_input = strip_tags($user_input); //removes html and javascript tags
$user_input = str_replace('<', '&lt;', $user_input); //replaces < with the ascii version

As I said before, I've heard there's some bugs in strip_tags so I run that str_replace just in case. I haven't had any problems with it.

The following will remove anything but letters, numbers, underscores, periods dashs, (, ), !, ?, spaces and a commas:

$user_input = preg_replace("/[^a-zA-Z0-9._ \(\)!?,]/", "", $user_input);

For example:

Code:
$string = 'This is just @ <test> (of) the sytem!! does, this work @ this time & can I break it * ??'; echo "Before: $string <p>"; $string = preg_replace("/[^a-zA-Z0-9._ \(\)!?,]/", "", $string); echo "After: $string";
The above will output:

Quote:
Before: This is just @ (of) the sytem!! does, this work @ this time & can I break it * ??

After: This is just test (of) the sytem!! does, this work this time can I break it ??
-the mole
 
Reply With Quote
  #6 (permalink)  
Old 05-10-2006, 11:25 PM
ATLien's Avatar
Contributing Member
 
Join Date: 10-13-03
Location: Atlanta, GA
Posts: 1,135
iTrader: 0 / 0%
Thanks you all for the input. I think I am going to go with the only allowing those characters I listed above, that will rule out html, javascript, and ascii translations of characters. Thanks for the help. I will keep reading this thread if you have any more comments.
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
 Charactors Show up when saving with M$ Expressions richardvan1 Web Design Lobby 2 10-26-2007 01:15 PM
Top Ten Google Words & Expressions peter_d Google Forum 0 07-12-2007 08:38 PM
Regular Expressions georgechristodoulou Coding Forum 9 12-01-2006 02:14 PM
PHP regular expressions DragonEye Coding Forum 1 11-28-2005 01:58 PM
Google and abbreviations/expressions baldlygo Google Forum 2 05-01-2004 06:08 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 05:38 AM.
Powered by vBulletin
Copyright © 2000-2013 Jelsoft Enterprises Limited.
Copyright © 2003 - 2013 Escalate Media LP




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.