I have recently been switching all of my old mysql_query calls to PDO's. I've encountered an issue that wasn't present in the mysql_query configuration. When I try to input a variable into a database with quotations, It appears as escapes in my database. I have disables magic quotes in my php.ini file.
$myString = "Enter 'one' now";
$sql=$pdo->prepare("UPDATE $tbl_name SET string=:myString WHERE etc...);
$sql->execute(array(':myString' => $myString));
This updates to
Enter \'one\' now
What I need is
Enter 'one' now
This wasn't happening before I switched to PDO. Is there a way around this without losing security?
Thanks!
You have magic quotes enabled on your server, the PHP documentation for it tells you various ways you can turn it off.
In regards to your comment: You can also try creating a php.ini file in your home directory with the following:
magic_quotes_gpc = off
magic_quotes_runtime = off
magic_quotes_sybase = off
If that doesn't work, then contact your host to address the issue.
Related
I have a CentOS 5 VPS and I have just installed my website there. But I have problems that when I'm inserting special characters into the password field like: ' (apostophe) or something like that, I always get the error, that this password is incorrect :/ I guess this is because of the Linux System. Am I right? Or maybe because of te sanitizing I'm doing?
I'd be gald if anyone could help me.
EDIT:
function array_sanitize(&$item) {
$item = htmlentities(strip_tags(mysql_real_escape_string($item)));
}
If your magic quote is enabled, then you should turn it off:
Edit these in your php.ini:
; Magic quotes
;
; Magic quotes for incoming GET/POST/Cookie data.
magic_quotes_gpc = Off
; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.
magic_quotes_runtime = Off
; Use Sybase-style magic quotes (escape ' with '' instead of \').
magic_quotes_sybase = Off
Or you can put this in .htaccess in document root:
php_flag magic_quotes_gpc Off
If you want to know about magic quote:
http://php.net/manual/en/security.magicquotes.php
hmm this sounds like a input escaping issue to me, or some setting in the website.
try to trace the path of the inputs to the actual query that executes and checks for a valid username/password. This is all i can write without actual code.
P.S i can only post answers that is why i am posting it like this.
i know this sounds really common and so trivial but , am having a challenge here. I have a web site with Zend/Doctrine and i use ckeditor for the backend management. after uploading the site i've realized that during edit testing the look and feel of the site is messed up.
with the help of firebug, i've seen that there are slashes all over the html. after inline edition, the look and feel came back to normal. There are so many files , i can't think of doing other decoding before outputting data from mysql.
What options do i have to solve this problem. the site is up already and i feel a bit unconfortable about this. Can anyone give a hint? thanks
It might be magic_quotes_gpc. Can you verify that it's turned off?
Here is a way to turn it off:
http://php.net/manual/en/security.magicquotes.disabling.php
Sets the magic_quotes state for GPC (Get/Post/Cookie) operations. When magic_quotes are on, all ' (single-quote), " (double quote), \ (backslash) and NUL's are escaped with a backslash automatically.
Also, are you using prepared statements? PHP PDO/MySQLI will escape automatically for you. Depends on the type of queries you're using.
It seems like you're data is getting double escaped before being inserted into your database. Are you using mysql_real_escape_string or addslashes before inserting data into the database? If so, maybe you want to use stripslashes before you insert your data like so:
mysql_real_escape_string(stripslashes($data));
Or else you could theoretically call stripslashes after you take the data out of the database:
stripslashes($data);
The second approach is less desirable, though. It would be better to have the data properly stored in the database.
I thank every one for the help. Really the accepted solution should be the one from #Stanislav Palatnik . just that it didn't work with my .htaccess. the hosting server was nice enough to put a php.ini in my public_html allowing me to change it. So +1 to #Stanislav Palatnik because he pointed out the issue. i also found interesting information i thought i would share in case someone found himself in my situation.
info from: http://support.godaddy.com/groups/web-hosting/forum/topic/how-to-turn-off-magic_quotes_gpc/
Yes – the solution below worked for me:
(1) First of all do not try to turn off the magic quotes in your .htaccess file, it won’t work on godaddy.
(2) Second, if you’re running PHP5 on your account, rename your php.ini file to php5.ini, make sure it’s in your root folder.
(3) Third, make sure all the lines in your php5.ini file end in a semi colon ;
(4) Fourth, add this line to your php5.ini file:
magic_quotes_gpc = Off;
on the same page someone said it shouldn't be only magic_quotes_gpc only but other ones aswell like shown below:
magic_quotes_gpc = Off;
magic_quotes_runtime = Off;
magic_quotes_sybase = Off;
Hope this helped someone. Special thanks to #Stanislav Palatnik
In case this is a magic quotes problem and as i recall you only having access to your application.ini, you might add the following and give it a try
phpSettings.magic_quotes_gpc = 0
phpSettings.magic_quotes_runtime = 0
This still requires your user / usergroup to be allowed to change default php settings ;)
<?php
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process)) {
foreach ($val as $k => $v) {
unset($process[$key][$k]);
if (is_array($v)) {
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stripslashes($k)];
} else {
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);}
?>
add this to your php page which has insert/update query :)
I send this string in a GET request
{"foo":[{"bo1":"*","bob":"*"}]}
but get it in PHP as
{\"foo\":[{\"bo1\":\"*\",\"bob\":"\*\"}]}
How do I get it as {"foo":[{"bo1":"*","bob":"*"}]} sending it as part of a query string (or how do I send it via GET method to get it properly)? (Note: I cannot clean it as I have no control over server side.)
Disable magic_quotes: it's deprecated. If you can't, you can always use stripslashes on the input:
$goodStr = stripslashes($_GET['badStr']);
Your php config have enabled magic_quotes_gpc, which causes automatic escaping of quotes and double quotes in all _GET, _POST, and _COOKIE superglobals.
If you do not need it, turn it off. If you do, then you should probably rewrite the code which relies on this behaviour, as it is depreciated, and will be removed in future verions of php.
You should turn it of in php.ini if possible.
Anyway, if you, for some reasons, cannot turn off this just use stripslashes($your_json);
If the server runs on Apache, create a file called .htaccess in the site root (the leading period is part of the filename). Put the following code in the file:
php_flag magic_quotes_gpc Off
Otherwise, you'll need to use stripslashes() every time.
$_GET['search'] = ucfirst(strtolower(str_replace("_"," ",urldecode($_GET['search']))));
For some reason it's adding slashes into the string similar to mysqL_escape_string, anyone got any ideas what would be causing it?
You have most probably magic_quotes_gpc set to on in php.ini. If you want to avoid that, make a check like this:
if (get_magic_quotes_gpc())
{
$mytext = stripslashes($your_text);
}
// and your further code....
Check to see if magic_quotes_gpc is enabled on your server. If this is enabled, PHP automatically escapes anything from _GET _POST or _COOKIES.
See: http://php.net/manual/en/security.magicquotes.php
It sounds like magic_quotes_gpc is turned on. You can get the setting with get_magic_quotes_gpc().
Today I run into some oddity with PHP, which I fail find a proper explanation for in the documentation. Consider the following code:
<?php
echo $_GET['t']. PHP_EOL;
?>
The code is simple - it takes a single t parameter on the url and outputs it back. So if you call it with test.php?t=%5Ca (%5c is a '\'), I expected to see:
\a
However, this is what I got:
$ curl http://localhost/~boaz/test.php?t=%5Ca
\\a
Notice the double slash. Can anyone explains what's going on and give recipe for retrieving the strings as it was supplied on the URL?
Thanks,
Boaz
PS. I'm using PHP 5.2.11
This happens, because you have the "magic quotes" switch in php.ini switched on. From the manual:
When on, all ' (single-quote), "
(double quote), \ (backslash) and NULL
characters are escaped with a
backslash automatically. This is
identical to what addslashes() does.
Read more about it here: http://php.net/manual/en/security.magicquotes.php
To make your script aware of any value of the "magic_quotes_gpc" setting in php.ini, you can write your script like this:
$d = $_GET["d"];
if (get_magic_quotes_gpc()) $d = stripslashes($d);
echo $d; //but now you are kind of vulnerable to SQL injections
//if you don't properly escape this value in SQL queries.
You can easily fix this using the strip_slashes() function. You should avoid magic quotes; they've been deprecated for security reasons.
open .htaccess file and put something like this
php_flag magic_quotes_gpc off
php_flag magic_quotes_runtime off