question about validation and sql injection

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.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old May 15th, 2008, 22:48
Junior Member
Join Date: Dec 2007
Location: auckland
Age: 33
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
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
PHP: Select all

$username $_POST["username"];
if( 
$username == "" || !eregi("^[a-zA-Z0-9_]+$"$username) )
{
$error.="User name cannot be blank or has special characters";

Question = how can i rewrite this php validation for username to meet the above criteria or is my validation correct

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:
htmlentities
addslashes
trim
mysql-real-escape-string
magic_quotes_gpc is ON
magic_quotes_runtime is OFF
magic_quotes_sybase is OFF
as i have not done preventing sql injection i am not sure what is the correct process.

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old May 15th, 2008, 23:23
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,269
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
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().
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old May 18th, 2008, 12:10
Junior Member
Join Date: Dec 2007
Location: auckland
Age: 33
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old May 18th, 2008, 14:56
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,269
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
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?
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old May 19th, 2008, 16:47
Junior Member
Join Date: Aug 2007
Location: uk
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old May 21st, 2008, 14:22
Up'n'Coming Member
Join Date: Jun 2007
Location: Birmingham, UK
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: question about validation and sql injection

Hi

As CloudedVision said this is a regex problem.

I would modify the line
Code: Select all
if( $username == "" || !eregi("^[a-zA-Z0-9_]+$", $username) )
to this
Code: Select all
if( $username == "" || !eregi("^[a-zA-Z0-9]+_?[a-zA-Z0-9]*$", $username) )
This checks for at least one alphanumeric character followed by an optional underscore followed by an arbitrary number of alphanumeric characters.

Let me know if it works
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 03:58.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42