PHP 转义查询字符串变量

发布于 2024-10-30 12:58:00 字数 427 浏览 0 评论 0原文

我在 Web 应用程序中创建了一个表单,该表单只有一个文本字段,并且该字段使用 GET 发布到 PHP 页面,但我观察到奇怪的行为。即,当我在本地服务器上测试它时,接收到的文本与文本字段中写入的一样,但是当我将其上传到在线服务器时,接收到的字符串会自动转义,这意味着所有单引号和双引号都会被转义。例如,如果我写 It's not true... 那么在 php 方面我会得到

$comment = $_REQUEST["comm"];
print $comment;
//will print It\'s not true... on my online server
//will print It's not true... on my local server

我还无法理解为什么会这样?是否有任何 PHP 设置可以自动转义查询字符串变量?

I have created a form in my web application which has only a single text field and that field is posted to a PHP page using GET, but I am observing strange behavior. i.e. when I test it on my local server, the text is received as it was written in the text field, but when I upload it to my online server, the received string is escaped automatically means, all single quotes and double quotes are escaped. e.g. If I write It's not true... then on php side I will get

$comment = $_REQUEST["comm"];
print $comment;
//will print It\'s not true... on my online server
//will print It's not true... on my local server

I am yet unable to under stand why is it so? Is there any PHP setting for escaping Query Strings variables automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是年少的欢喜 2024-11-06 12:58:00

您已启用“魔术引号”。它们是一个可怕的错误功能,幸运的是在下一版本的 PHP 中被删除。 PHP 手册有一个指南来禁用它们。

简而言之,您需要在 php.ini 文件中将以下配置项设置为 Off

  • magic_quotes_gpc
  • magic_quotes_runtime
  • magic_quotes_sybase

具体来说,您的问题似乎与 magic_quotes_gpc< /code> - “gpc”部分是“GET、POST 和 COOKIE”的缩写 - 但最好将它们全部禁用。

You have "magic quotes" enabled. They're a terrible misfeature which are luckily being removed in the next version of PHP. The PHP manual has a guide to disabling them.

In short, you need to set the following configuration items to Off in your php.ini file:

  • magic_quotes_gpc
  • magic_quotes_runtime
  • magic_quotes_sybase

Specifically, your problem appears to be with magic_quotes_gpc - the "gpc" portion being short for "GET, POST, and COOKIE" - but it's good practice to keep all of them disabled.

懒的傷心 2024-11-06 12:58:00

代码会告诉你你需要的一切......

function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_real_escape_string"); // i.e. PHP >= v4.3.0
if ($new_enough_php) { // PHP v4.3.0 or higher
    // undo any magic quote effects so mysql_real_escape_string can do the work
    if ($magic_quotes_active) {
        $value = stripslashes($value);
    }
    $value = mysql_real_escape_string($value);
} else { // before PHP v4.3.0
    // if magic quotes aren't already on then add slashes manually
    if (!$magic_quotes_active) {
        $value = addslashes($value);
    }
    // if magic quotes are active, then the slashes already exist
}
return $value;
}

创建上​​面的函数并将值传递给这个函数

,然后调用这些值,就像

$yourVar = mysql_prep($_POST['yourControlName']);

我希望你可以通过注释得到所有解释......

Code will tell you every thing what you need..

function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_real_escape_string"); // i.e. PHP >= v4.3.0
if ($new_enough_php) { // PHP v4.3.0 or higher
    // undo any magic quote effects so mysql_real_escape_string can do the work
    if ($magic_quotes_active) {
        $value = stripslashes($value);
    }
    $value = mysql_real_escape_string($value);
} else { // before PHP v4.3.0
    // if magic quotes aren't already on then add slashes manually
    if (!$magic_quotes_active) {
        $value = addslashes($value);
    }
    // if magic quotes are active, then the slashes already exist
}
return $value;
}

create above function and pass-on values to this function

and then call the values like

$yourVar = mysql_prep($_POST['yourControlName']);

I hope you may get every thing explained via comments...

扎心 2024-11-06 12:58:00

我认为它是 php.ini 文件中的设置。您可以调用 PHP 函数来禁用它,但那时为时已晚。

I think its a setting within the php.ini file. You can call a PHP function to disable it, but by then it's too late.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文