Current location: Hot Scripts Forums » Programming Languages » PHP » How to protect from sql injection?


How to protect from sql injection?

Reply
  #1 (permalink)  
Old 08-30-08, 10:31 PM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 187
Thanks: 2
Thanked 0 Times in 0 Posts
How to protect from sql injection?

Hello,
I want to know how can I protect from sql injection?
What is the best way?
Is there anyway to add one function for all queries and where get sql injection?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-31-08, 08:24 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by Hamed View Post
Hello,
I want to know how can I protect from sql injection?
What is the best way?
Is there anyway to add one function for all queries and where get sql injection?
If you search this forum you'll find this subject has been discussed many times. Sanitizing input is a multi-step process and no one single step is "it". Validate all incoming data for type, remove potentially malicious characters, and strip common exploit code. After doing that, use mysql_real_escape_string() for all queries to the database or use a custom function that performs the same actions.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]

Last edited by End User; 02-26-10 at 08:57 AM. Reason: Added <?php tags for proper highlighting.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-01-08, 11:40 PM
loveloop loveloop is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
you can use htmlspecialchars to protect from sql injection

$input = htmlspecialchars($input,ENT_QUOTES);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-02-08, 12:26 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by End User View Post
Sanitizing input is a multi-step process and no one single step is "it".
...just to be sure loveloop read the previous message.
__________________
The toxic ZCE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-02-08, 01:19 AM
loveloop loveloop is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Keith View Post
...just to be sure loveloop read the previous message.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 09-02-08, 10:04 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by loveloop View Post
you can use htmlspecialchars to protect from sql injection
WRONG. You write a script with that as your only protection and in 10 minutes I'll be having my way with your server like a drunken prom date.

Seriously, don't think for one second that htmlspecialchars() is going to protect you. It won't.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 09-02-08, 02:45 PM
woyrz's Avatar
woyrz woyrz is offline
Newbie Coder
 
Join Date: Dec 2003
Location: montreal
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
firsth of all you need to know exaclty what you are trying to do.

STRING VALIDATION
I you expected to received number cast your sting as int so that way php will reject any alpha chars.

length and restriction can offent be applied to your string limiting what you will store.

if the string doesn't match what I'm expecting I drop everything and display an error message.

STRING CLEANING
htmlspecials... and addslashes are powerful but not alone.

I often user htmlspecialchars with the ENT_QUOTES options

THE SQL
As for the SQL i used prepare statement and PDO.
If you used a FrameWork they give you an other layer of security if you dont by pass them.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 09-03-08, 03:51 PM
Mike Mike is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by End User View Post
WRONG. You write a script with that as your only protection and in 10 minutes I'll be having my way with your server like a drunken prom date.

Seriously, don't think for one second that htmlspecialchars() is going to protect you. It won't.
I had a good laugh at this... I must say End User... VERY good choice of words. Back on the topic though... he is absolutely correct... 10 minutes or less you will be screwed.
__________________
Mike
ImperialBB Development Coordinator
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQl Injection through ASP and MS SQl 2000 cancer10 ASP 5 12-18-07 02:30 PM
Noah Classifieds Open to attacks like SQL injection etc. websmart PHP 4 09-17-06 04:09 AM
SQL injection and addslashes() bd_coder PHP 1 06-08-06 10:06 AM
Securing forms from SQL Injection Vineman PHP 2 01-26-06 07:14 AM
change my field in this example sal21 ASP 3 07-14-03 03:49 AM


All times are GMT -5. The time now is 06:39 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.