Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-16-2006, 10:53 PM   PM User | #1
kaisellgren
Regular Coder

 
Join Date: Jan 2006
Location: Finland, Hollola
Posts: 285
Thanks: 8
Thanked 0 Times in 0 Posts
kaisellgren is an unknown quantity at this point
Question How would you check for illegal letters from form field?

Hi,

If user is asked for a username and password in a form. How would you do a check in PHP to make sure that username and password has no illegal characters such as, new line, tab, / or \, *, -, ., ,,, ", ', so on...
__________________
PHP 5 & MySQL 5 (Y)
kaisellgren is offline   Reply With Quote
Old 06-16-2006, 11:10 PM   PM User | #2
Kid Charming
Regular Coder

 
Join Date: Jun 2005
Posts: 804
Thanks: 0
Thanked 0 Times in 0 Posts
Kid Charming is an unknown quantity at this point
It depends on what you wish to consider illegal. ctype_alnum() checks for only alphanumeric characters. If you want limited acceptance of other characters, you can use regular expressions.
Kid Charming is offline   Reply With Quote
Old 06-17-2006, 01:06 AM   PM User | #3
Nicklas
New Coder

 
Join Date: Jun 2006
Location: Sweden
Posts: 49
Thanks: 0
Thanked 3 Times in 3 Posts
Nicklas is on a distinguished road
use regular expressions. The example below, only accepts a-z and numbers, all other chars are invalid.

ex
PHP Code:
if (preg_match('/^[a-z0-9]$/i'$username)) {
// Ok
} else {
// Not ok


Last edited by Nicklas; 06-17-2006 at 01:42 AM..
Nicklas is offline   Reply With Quote
Old 06-17-2006, 01:30 AM   PM User | #4
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
That regex will only match single character input.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 06-17-2006, 01:36 AM   PM User | #5
Kid Charming
Regular Coder

 
Join Date: Jun 2005
Posts: 804
Thanks: 0
Thanked 0 Times in 0 Posts
Kid Charming is an unknown quantity at this point
Regular expressions are relatively resource intensive. Generally speaking, if you can do something without using regex, you should. If you want your users to just use letters, use ctype_alpha(). If you only want letters and numbers, use ctype_alnum(). If you want letters, numbers, and a few select extra characters, such as underscores, dollar signs, etc., then you'll need to use a regex.
Kid Charming is offline   Reply With Quote
Old 06-17-2006, 01:41 AM   PM User | #6
Nicklas
New Coder

 
Join Date: Jun 2006
Location: Sweden
Posts: 49
Thanks: 0
Thanked 3 Times in 3 Posts
Nicklas is on a distinguished road
Oops, missed a + char

PHP Code:
if (preg_match('/^[a-z0-9]+$/i'$username)) {
// Ok
} else {
// Not ok

If you wanna limit the length of the $username and make sure it's, for example, at least 4 chars and not longer than 10 chars, then replace the + with {minimum, maximum}

ex
PHP Code:
if (preg_match('/^[a-z0-9]{4,10}$/i'$username)) {
// Ok
} else {
// Not ok

Nicklas is offline   Reply With Quote
Old 06-17-2006, 02:04 AM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,448
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
preg_match('/^[a-z0-9]+$/i', $username)

and

ctype_alnum($username)

do exactly the same thing except that the second one runs a lot faster since it runs compiled code instead of interpreted script. It also avoids the possibility of a typo (such as leaving out the +).
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 06-17-2006, 11:30 AM   PM User | #8
kaisellgren
Regular Coder

 
Join Date: Jan 2006
Location: Finland, Hollola
Posts: 285
Thanks: 8
Thanked 0 Times in 0 Posts
kaisellgren is an unknown quantity at this point
Okay. I'm little confused about which one to use, preg_match or ctype_alnum... I just want that user can ONLY put a,b,c,d,...,z and 0,1,2,3,4,5,6,7,8,9 nothing else. If user types any other characters, then the code will do exit;

Yeah. A minimum 4 characters would be good and some like max 16 characters...

Well, if ctype_alnum is faster than preg_match, can I check for the lenght of the input with ctype_alnum expression?

Thanks for help!
__________________
PHP 5 & MySQL 5 (Y)
kaisellgren is offline   Reply With Quote
Old 06-17-2006, 03:45 PM   PM User | #9
Nicklas
New Coder

 
Join Date: Jun 2006
Location: Sweden
Posts: 49
Thanks: 0
Thanked 3 Times in 3 Posts
Nicklas is on a distinguished road
Something like this...
PHP Code:
if (ctype_alnum($username)) {

    if (
strlen($username) >= && strlen($username) <= 16) {
    
// Username is Ok and within the requested length
    
}
    
// Username is Ok, but NOT within the requested length
    
}

} else {
// Bad username!!!

Nicklas is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 03:22 AM.


Advertisement
Log in to turn off these ads.