473,463 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sql injection

i have implemented a way to avoid sql injection from the php website from this url
http://in.php.net/mysql_real_escape_string from the "Example #3 A "Best Practice" query" section of this page

following are the steps i have followed after the form values are submitted to a php file.

step 1.
Expand|Select|Wrap|Line Numbers
  1. if(get_magic_quotes_gpc()) 
  2. {
  3. $username = stripslashes($_POST["username"]);
  4. .........
  5. }
  6.  
  7. else
  8. {
  9. $username = $_POST["username"];
  10. .........
  11. }
  12.  
step 2.
Expand|Select|Wrap|Line Numbers
  1. $conn = mysql_connect($hostname, $user, $password);
  2.  
step 3.
Expand|Select|Wrap|Line Numbers
  1. $insertquery = sprintf("INSERT INTO table (`username`, ...) VALUES ('%s', ...)", mysql_real_escape_string($username, $conn), 
  2.  
  3. ...);
  4.  
step 4.
Expand|Select|Wrap|Line Numbers
  1.     if(!$conn)
  2.     { 
  3. header("Location: http://website/dberror.html"); 
  4. exit;
  5.     }
  6.  
  7.     else
  8.     {
  9. mysql_select_db($database, $conn);
  10.  
  11. $insertqueryresult = mysql_query($insertquery);       
  12.  
  13.  
  14.     if(!$insertqueryresult)    {        
  15.     header("Location: http://website/error.html"); 
  16.     exit;                     }     
  17.  
  18.     }
  19.  
with the above method i am able to insert values into the table even with if i enter the ' special character which can cause

problems.

i have also used a simple sql insert query like
Expand|Select|Wrap|Line Numbers
  1. $insertquery = "INSERT INTO table(username, ...) VALUES ('$username', ...)";
  2.  
when i used this simple insert query and if i entered ' in the form and submitted the form the php file is unable to process

the information entered because of the ' character and as per the code error.html file is being displayed where as if i use
Expand|Select|Wrap|Line Numbers
  1. $insertquery = sprintf("INSERT INTO table (`username`, ...) VALUES ('%s', ...)", mysql_real_escape_string($username, $conn), 
  2.  
  3. ...);
  4.  
even if i enter any number of ' characters in more than 1 form field data is being inserted into the table

a)
so i am thinking that the steps i have taken from the php site is correct and the right way to avoid sql injection though

there are several ways to avoid sql injection.

b)
for example if i enter data in the form as = abc'''def for name, the data in the table for the name field is being written as

abc'''def

based on how i have written the steps to avoid sql injection is this the right way for the data to be stored with '

characters along with the data example as i mentioned = abc'''def

please answer the questions a) and b) if there is something else i need to do please suggest what needs to be done exactly

and at which step.

any help will be greatly appreciated.

thanks.
May 29 '08 #1
1 1489
pbmods
5,821 Expert 4TB
Heya, Runway.

mysql_real_escape_string() essentially escapes quotes and comment characters (such as -- and /*) by prepending them with slashes (e.g., "abc'''123" becomes "abc\'\'\'123").

Since these characters can be changed (though very rarely are), mysql_real_escape_string() is preferred over addslashes() or addcslashes().

For maximum security and more organized code, you should consider switching to MySQLi and its ability to use prepared statements.
May 30 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: NotGiven | last post by:
Steve wrote, > "And read up on "sql injection" attacks (use your favorite search > engine). As indicated, validate input. e.g. if you expert $_GET > to be integer, then do > > $a =...
1
by: Cogswell | last post by:
I am working on an ecommerce app and want to be able to take my entire POST results as one item (or iterate through them) and check for any malicious SQL INJECTION items. After checking/escaping...
11
by: Bă§TăRĐ | last post by:
I have been working on this particular project for a little over 2 weeks now. This product contains between 700-900 stored procedures to handle just about all you can imagine within the product. I...
10
by: bregent | last post by:
I've seen plenty of articles and utilities for preventing form injections for ASP.NET, but not too much for classic ASP. Are there any good input validation scripts that you use to avoid form...
8
by: stirrell | last post by:
Hello, One problem that I had been having is stopping email injections on contact forms. I did some research, read up on it and felt like I had created a working solution. I hadn't gotten any...
7
by: | last post by:
There are assorted "SQL Injection vulnerability assessment tools" out there. They scan your site and send your report. They also take your money. We don't have the money so I was wondering if I...
3
by: =?Utf-8?B?Um9kbmV5IFZpYW5h?= | last post by:
IIS 6 SQL Injection Sanitation ISAPI Wildcard at http://www.codeplex.com/IIS6SQLInjection I created an ISAPI dll application to prevent SQL Injection attempts by intercepting the HTTP requests...
2
by: Sudhakar | last post by:
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...
12
by: shank | last post by:
I've been hit again using DW, parameterized queries and stored procedures. I'm guessing I was not strict enough with character counts and allowing to long of a string to pass. Aside from that,...
2
by: Brian Bozarth | last post by:
This is weird, I'm pretty familiar with SQL Injection - but we're getting these weird injection that is writing in the default document or home page. What it's doing is putting in script code at...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.