944,144 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7207
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 17th, 2007
0

php whitelist form validation??

Expand Post »
Hi!!!

I need to check a form for illegal characters through a whitelist(list of allowed characters) with php. A function that could do this for me would be helpful...I can't find one on google...

I really have no idea how to do this....


thanks!!!!
~Kodiak
Reputation Points: 15
Solved Threads: 0
Light Poster
kodiak is offline Offline
38 posts
since Jul 2007
Jul 17th, 2007
0

Re: php whitelist form validation??

preg_match();

You'll need to know regular expressions to use it.
Last edited by stymiee; Jul 17th, 2007 at 6:53 pm.
Moderator
Reputation Points: 161
Solved Threads: 38
He's No Good To Me Dead
stymiee is offline Offline
1,422 posts
since May 2006
Jul 17th, 2007
0

Re: php whitelist form validation??

If the whitelist is entirely comprised of single characters, you can probably use
PHP Syntax (Toggle Plain Text)
  1. preg_replace( "[^#$%]", "", $stringToFilter );
to replace any occurrence of "#","$",or "%" with an empty string. Just put whatever characters you need to strip in there. If it's not a simple single character situation (i.e. you need to strip something like "<p>") then you'll have to go deeper into regex land to get there.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 17th, 2007
0

Re: php whitelist form validation??

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
If the whitelist is entirely comprised of single characters, you can probably use
PHP Syntax (Toggle Plain Text)
  1. preg_replace( "[^#$%]", "", $stringToFilter );
to replace any occurrence of "#","$",or "%" with an empty string. Just put whatever characters you need to strip in there. If it's not a simple single character situation (i.e. you need to strip something like "<p>") then you'll have to go deeper into regex land to get there.
Actually, sorry, that pattern is bad, use this instead
PHP Syntax (Toggle Plain Text)
  1. preg_replace( "/[#$%]/", "", $stringToFilter );
(It was too late to edit the other post)
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 17th, 2007
0

Re: php whitelist form validation??

isn't there a way to make characters that are allowed(whitelist)
instead of characters that aren't allowed(blacklist)??
Reputation Points: 15
Solved Threads: 0
Light Poster
kodiak is offline Offline
38 posts
since Jul 2007
Jul 18th, 2007
0

Re: php whitelist form validation??

Sure, you can regular expressions to look for anything you want.
Moderator
Reputation Points: 161
Solved Threads: 38
He's No Good To Me Dead
stymiee is offline Offline
1,422 posts
since May 2006
Jul 18th, 2007
0

Re: php whitelist form validation??

php Syntax (Toggle Plain Text)
  1. preg_replace( "/[^a-zA-Z0-9_]/", "", $stringToFilter );
i guess this could work for your case
replacing everything that is not char. or digit with whitespace
Reputation Points: 18
Solved Threads: 9
Junior Poster
w_3rabi is offline Offline
160 posts
since Dec 2006
Jul 18th, 2007
0

Re: php whitelist form validation??

Click to Expand / Collapse  Quote originally posted by w_3rabi ...
php Syntax (Toggle Plain Text)
  1. preg_replace( "/[^a-zA-Z0-9_]/", "", $stringToFilter );
i guess this could work for your case
replacing everything that is not char. or digit with whitespace
That was what I was getting at, but I see I dropped the "^" when I retyped my earlier expression. It should work fine as a single character mask.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 18th, 2007
0

Re: php whitelist form validation??

well what u did was the black list thing
and what i did is white one
it may look the same
but it is not!
Reputation Points: 18
Solved Threads: 9
Junior Poster
w_3rabi is offline Offline
160 posts
since Dec 2006
Jul 18th, 2007
0

Re: php whitelist form validation??

Click to Expand / Collapse  Quote originally posted by w_3rabi ...
well what u did was the black list thing
and what i did is white one
it may look the same
but it is not!
Yes, I got it turned around, which I mentioned previously

Anyway, it does not matter. I posted to support that what you posted should work for him, he just needs to tune the pattern to include his acceptable characters
Last edited by Ezzaral; Jul 18th, 2007 at 1:19 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Email addrress in Sessions
Next Thread in PHP Forum Timeline: Retrieve URL from address bar





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC