This is a discussion on "question about validation and sql injection" within the PHP Forum section. This forum, and the thread "question about validation and sql injection are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
question about validation and sql injection
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
question about validation and sql injection
A) validating username in php
as part of a registration form a user fills there desired username and this is stored in a mysql. there are certain conditions for the username. a) the username should only begin either letters or numbers, and Underscore character example = user123, 123user, u_ser123, user_123 = completely case insensitive b) a user may choose not to have an underscore or numbers sometimes. example = username presently my validation for username is
B) preventing sql injection till now i have been capturing the form values and directly inserting into the table without considering sql injection however for this project as it is for a forum i would like to implement prevention of sql injection. from what i have read about preventing sql injection there are several steps that need to be followed, Quote:
Question = a) please advice a step by step process of how to go about avoiding the sql injection before the insert sql query is executed starting from $username = $_POST["username"]; till the insert into tablename(field1, field2) values($value1, $value2) SQL query is executed which will prevent sql injection even if the user enters any special characters while filling the form. b) should i consider the setting of magic quotes as in should it be ON or OFF or should i ignore it if so should it be ON or OFF c) also with the prevention methods if a user types a special character in the data will that character be written in the table as a escaped character or how does it store those special characters d) a very important point here, i have a feature where a user can check if a username is available or not. so while storing a username if the username is stored as john\smith in mysql and if the user is searching for johnsmith this would not match, so even in the table the username should be stored without slashes as i have to read the username and compare with what the user has typed to see if they both are same or different. please advice if i have missed any other steps to prevent sql injection. thanks a lot for your help. Last edited by Aso; May 17th, 2008 at 13:37. Reason: Made readable |
|
|
|
||||
|
Re: question about validation and sql injection
for a, can't you just test it out to see if it works?
and for b, magic quotes can cause problems, but if its off use mysql_real_escape_string(). if its on, turn it off and use mysql_real_escape_string().
__________________
echo "Take it easy, ".$CloudedVision; .links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; } <quote>"I think it's wrong that only one company makes the game Monopoly" - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
|
|
|||
|
Re: question about validation and sql injection
with the following validation
$username = $_POST["username"]; if( $username == "" || !eregi("^[a-zA-Z0-9_]+$", $username) ) { $error.="User name cannot be blank or has special characters"; } this works fine however 1) if a user types "underscore" more than once the validation is accepting 2 underscores which i do not want. i want the user to enter only 1 underscore and letters and numbers can be repeated. 2) also if a user enters "underscore" as the first character it is still accepting. ex: _username. i do not want the underscore to be the first character how can i rewite the above validation to mee my requirement. |
|
||||
|
Re: question about validation and sql injection
the problem with that is that regular expressions are not PHP, and we don't have a forum for regex, so the Webforumz Cafe would be the best place. Would you like me to move it there?
__________________
echo "Take it easy, ".$CloudedVision; .links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; } <quote>"I think it's wrong that only one company makes the game Monopoly" - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
|
|
|||
|
Re: question about validation and sql injection
I have read its ok just to have alpha numeric input making it much less complicated. It will stop any sql injection and bots trying to find the password will have to search each word in there dictionary hundreds of times
|
|
|||
|
Re: question about validation and sql injection
Hi
As CloudedVision said this is a regex problem. I would modify the line
Let me know if it works |
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| question about database injection | sudhakararaog | PHP Forum | 0 | Mar 17th, 2008 01:00 |
| VERY IMPORTANT question about validation using php | sudhakararaog | PHP Forum | 2 | Mar 9th, 2008 15:29 |
| SQL injection prevention | AdRock | PHP Forum | 3 | Sep 6th, 2007 13:55 |
| SQL Injection Security PHP | nate2099 | Databases | 7 | Jul 14th, 2007 13:58 |
| ohol-injection.com | rocket468 | Free Web Site Critique | 2 | Oct 27th, 2006 00:03 |