dbingham Posted June 24, 2008 Share Posted June 24, 2008 I've discovered that the post method seems to be auto-escaping my strings for me. I've been working on a simple PHP/MySQL front end/back end combination and have been researching security for said combination. Every thing I've looked at said that I should run all my strings through mysql_real_escape_string() before sending them to mysql_query() to as a preventative against SQL Injection attacks. However, I've discovered that the post method itself seems to do the job of mysql_real_escape_string() all on it's own. My strings to seem to be escaped already when they're in the $_POST array! These are strings entered into text fields in a form that is then sent to the processing script via the post method. When they arrive /, ' and " are all escaped. And when I run the strings through mysql_real_escape_string() the function then double escapes everything, which results in a bunch of / being inserted into my data. Does anyone know if POST is supposed to do this? Is there some setting I can use to turn it off and on? Is POST's escaping as secure and effective as mysql_real_escape_string? I did a few precursory searches for threads existing on this topic and found none, and I've found no mention of this in the documentation anywhere. I'm really rather baffled. Quote Link to comment https://forums.phpfreaks.com/topic/111731-solved-post-method-automatically-escaping-strings/ Share on other sites More sharing options...
Lodius2000 Posted June 24, 2008 Share Posted June 24, 2008 here here, i am having the same problem class="class" as part of a tag in my db is stored as class=///"class///" annoying I have no idea how to fix it Quote Link to comment https://forums.phpfreaks.com/topic/111731-solved-post-method-automatically-escaping-strings/#findComment-573545 Share on other sites More sharing options...
dbingham Posted June 24, 2008 Author Share Posted June 24, 2008 I actually discovered what appears to be the answer shortly after posting. The issue is magic_quotes. I found I had magic_quotes_gpc enabled on my server (it's apparently enabled by default pre-6.0 but won't be enabled by default after that). I went and disabled it, but I'm not wondering how many of my third party PHP/MySQL applications depended on it for escaping or if they handled it well. Gonna have to go through and look at their code. Quote Link to comment https://forums.phpfreaks.com/topic/111731-solved-post-method-automatically-escaping-strings/#findComment-573551 Share on other sites More sharing options...
KevinM1 Posted June 24, 2008 Share Posted June 24, 2008 It sounds as though you both have Magic Quotes turned on. Try the following function: <?php function myEscape($value) { return get_magic_quotes_gpc() ? mysql_real_escape_string(stripslashes($value)) : mysql_real_escape_string($value); } $name = myEscape($_POST['name']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111731-solved-post-method-automatically-escaping-strings/#findComment-573554 Share on other sites More sharing options...
DarkWater Posted June 24, 2008 Share Posted June 24, 2008 No application should rely on magic_quotes_gpc. And also, they're not off by default in PHP6...they don't even EXIST in PHP6. And yes, use Nightslyr's code so it's safe on any server. Obviously you should rename the function though so you know what it does. Quote Link to comment https://forums.phpfreaks.com/topic/111731-solved-post-method-automatically-escaping-strings/#findComment-573557 Share on other sites More sharing options...
dbingham Posted June 24, 2008 Author Share Posted June 24, 2008 It sounds as though you both have Magic Quotes turned on. Try the following function: <?php function myEscape($value) { return get_magic_quotes_gpc() ? mysql_real_escape_string(stripslashes($value)) : mysql_real_escape_string($value); } $name = myEscape($_POST['name']); ?> Will do. Thanks a bunch. Quote Link to comment https://forums.phpfreaks.com/topic/111731-solved-post-method-automatically-escaping-strings/#findComment-573561 Share on other sites More sharing options...
Lodius2000 Posted June 24, 2008 Share Posted June 24, 2008 can we just turn of magic quotes in php.ini? Quote Link to comment https://forums.phpfreaks.com/topic/111731-solved-post-method-automatically-escaping-strings/#findComment-573566 Share on other sites More sharing options...
DarkWater Posted June 24, 2008 Share Posted June 24, 2008 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/111731-solved-post-method-automatically-escaping-strings/#findComment-573568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.