Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 05-29-2008, 03:44 PM sql injection
Ultra Talker

Posts: 254
Trades: 0
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.

if(get_magic_quotes_gpc())
{
$username = stripslashes($_POST["username"]);
.........
}

else
{
$username = $_POST["username"];
.........
}

step 2.

$conn = mysql_connect($hostname, $user, $password);

step 3.

$insertquery = sprintf("INSERT INTO table (`username`, ...) VALUES ('%s', ...)", mysql_real_escape_string($username, $conn),

...);

step 4.

if(!$conn)
{
header("Location: http://website/dberror.html");
exit;
}

else
{
mysql_select_db($database, $conn);

$insertqueryresult = mysql_query($insertquery);


if(!$insertqueryresult) {
header("Location: http://website/error.html");
exit; }

}

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

$insertquery = "INSERT INTO table(username, ...) VALUES ('$username', ...)";

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

$insertquery = sprintf("INSERT INTO table (`username`, ...) VALUES ('%s', ...)", mysql_real_escape_string($username, $conn),

...);

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.
sudhakararaog is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-29-2008, 04:43 PM Re: sql injection
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
There are some lengthy discussions on this forum regarding SQL injection and protecting yourself from it. You should do a quick search for "sql injection" and check out those threads which answer your questions.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-29-2008, 07:04 PM Re: sql injection
Extreme Talker

Posts: 189
Trades: 0
check out this site on how to do a good prevention from it.

http://www.tizag.com/mysqlTutorial/m...-injection.php
simster is offline
Reply With Quote
View Public Profile
 
Old 05-29-2008, 07:21 PM Re: sql injection
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Stored procedures are the right way to avoid SQL Injection attacks. Escaping and the like, you're needlessly complicating your application, and in most cases, you're preventing users from sending input that could be genuinely valuable, like a regular expression string.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 05-29-2008, 10:50 PM Re: sql injection
upstarter's Avatar
Average Talker

Posts: 26
Name: Starr Horne
Trades: 0
It looks like you're reinventing a lot of what any database will give you for free. The easiest way to avoid SQL injection is just to use bind parameters. Do that and you can free up enough time to go after XSS attacks.
__________________
my company:
Please login or register to view this content. Registration is FREE


my blog:
Please login or register to view this content. Registration is FREE
upstarter is offline
Reply With Quote
View Public Profile Visit upstarter's homepage!
 
Reply     « Reply to sql injection
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21870 seconds with 12 queries