Solved

Protection against SQL injection while magic quote is off

Posted on 2007-04-05
9
215 Views
Last Modified: 2012-05-05
If magic quotes is turned off, how can you protect against SQL injection in this case?

I tried this code suggested by security book:

if (get_magic_quotes_gpc()) {
    $input = array(&$_GET, &$_POST, &$_COOKIE, &$_ENV, &$_SERVER);
    while (list($k,$v) = each($input)) {
        foreach ($v as $key => $val) {
            if (!is_array($val)) {
            $input[$k][$key] = stripslashes($val);
            continue;
            }
        $input[] =& $input[$k][$key];
        }
    }
    unset($input);
}

But it didnt do it!

Thanks for your help!
0
Comment
Question by:bprof2007
  • 4
  • 3
  • 2
9 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 18859537
Pass all your inputs through the function htmlentities with the ENT_QUOTES option.

$safeinput = htmlentities($input,ENT!QUOTES):
0
 
LVL 27

Expert Comment

by:yodercm
ID: 18859541
Sorry for the typo.

$safeinput = htmlentities($input,ENT_QUOTES);
0
 
LVL 28

Expert Comment

by:gamebits
ID: 18859565
You van also use

mysql_real_escape_string()

http://ca.php.net/manual/en/function.mysql-real-escape-string.php
0
 
LVL 27

Expert Comment

by:yodercm
ID: 18859594
Keep in mind that mysql_real_escape_string is for MySQL commands, it is not a general php function that would work for an array.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:bprof2007
ID: 18859621
Thanks for the prompt response. I've tried it and it gave me the same results:

test's test ''' test'''

But this is how it should look when magic quotes is on:

test\'s test \'\'\' test\'\'\'
0
 
LVL 28

Expert Comment

by:gamebits
ID: 18859666
0
 
LVL 1

Author Comment

by:bprof2007
ID: 18859723
Thanks gamebits,

I tried

mysql_real_escape_string()

but when magic quotes is off it does NOT work. I mean it gives me this:

test's test ''' test'''

Instead of this:

test\'s test \'\'\' test\'\'\'
0
 
LVL 27

Accepted Solution

by:
yodercm earned 250 total points
ID: 18859771
htmlentities does NOT add slashes into your input.  It changes the string to use the & html codes for all special characters.  That's what makes it so safe and so convenient for things you want to print but still be safe.   So when you print the results, you'll still see the quotes, but if you look in the database for example, you'll see &#34 or &#39.  

http://www.ascii.cl/htmlcodes.htm

0
 
LVL 1

Author Comment

by:bprof2007
ID: 18859834
Thank you yodercm. I need something like that.

Thanks gamebits for your contribution.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now