当我的 PHP 脚本从 AJAX POST 请求接收数据时,$_POST
变量被转义。真正奇怪的是,这只发生在我的生产服务器上(在 Linux 上运行 PHP 5.2.12),而不是在我的本地服务器上(在 Windows 上运行 PHP 5.3.1)。
这是 AJAX 代码:
var pageRequest = false;
if(window.XMLHttpRequest) pageRequest = new XMLHttpRequest();
else if(window.ActiveXObject) pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
pageRequest.onreadystatechange = function() { }
var q_str = 'data=' + " ' ";
pageRequest.open('POST','unnamed_page.php',true);
pageRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
pageRequest.setRequestHeader("Content-length", q_str.length);
pageRequest.setRequestHeader("Connection", "close");
pageRequest.send(q_str);
发生这种情况有什么原因吗?我应该如何解决这个问题,以便它在两台服务器上都能正常工作?
编辑:我对 magic_quotes 有以下设置:
Local Master
magic_quotes_gpc On On
magic_quotes_runtime Off Off
magic_quotes_sybase Off Off
最佳答案
您可能在 Linux 服务器上启用了魔术引号:magic_quotes
When magic_quotes are on, all ' (single-quote), " (double quote), \ (backslash) and NUL's are escaped with a backslash automatically.
它们是禁用的好东西,因为无论如何它们将从 PHP 6 中删除。 您还应该能够在脚本中禁用它们:set-magic-quotes-runtime 您不能停用负责在运行时转义 POST 数据的 magic_quotes 部分。如果可以,请在 php.ini 中禁用它。如果你不能这样做,请检查是否启用了 magic_quotes,并对从 POST 获取的任何内容执行 stripslashes():
if (get_magic_quotes_gpc())
$my_post_var = stripslashes($_POST["my_post_var"]);
关于php - 为什么 $_POST 变量在 PHP 中被转义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2496455/