转义引号的函数无法正常工作

发布于 2024-08-10 23:01:12 字数 647 浏览 6 评论 0原文

我试图找出为什么这个功能不能正常工作。

每次我编辑条目时,它都会添加一个额外的 \

在线服务器具有以下设置:

magic_quotes_gpc On 
magic_quotes_runtime Off 
magic_quotes_sybase Off

这是代码:

function esc($s)
{
  if (get_magic_quotes_gpc()) {
    if (ini_get('magic_quotes_sybase'))
      $s = str_replace("''", "'", $s);
    else
      $s = stripslashes($s);
  } //if 
  return mysql_real_escape_string($s);
}

编辑说明:

我尝试完全删除此功能以查看它的作用......并且它做了同样的事情,所以我意识到addslashes 也在代码中用于同样的事情。

额外的 \ 之所以存在,是因为 magic_quote 处于 ON

I'm trying to figure out why this function does not work correctly.

It's adding an extra \ every time I edit my entries.

Online server has these settings:

magic_quotes_gpc On 
magic_quotes_runtime Off 
magic_quotes_sybase Off

Here is the code:

function esc($s)
{
  if (get_magic_quotes_gpc()) {
    if (ini_get('magic_quotes_sybase'))
      $s = str_replace("''", "'", $s);
    else
      $s = stripslashes($s);
  } //if 
  return mysql_real_escape_string($s);
}

Edit note:

I have tried completely removing this function to see what it does... and it does the same thing, so I have realized that addslashes is also use in the code for the same thing.

The extra \ were there because magic_quote was ON

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

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

发布评论

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

评论(3

唔猫 2024-08-17 23:01:12

你的功能没有什么意义。如果启用了魔术引号(例如,输入被转义),则您将取消转义它。如果它没有打开,你就逃避它。因此,您会得到不同的结果,具体取决于您是否启用了魔术引用。

无论如何,依赖魔术引号是一种非常糟糕的做法。您应该:

  1. 全局禁用魔术引号或逆转其效果
  2. 要么 转义字符串 当您构建 SQL 查询(更好)使用准备好的语句
  3. 当您从数据库取回它时, unescape/strip/whatever 任何内容。

Your function makes little sense. If magic quotes is on (eg. input is escaped), you unescape it. If it's not on, you escape it. So you'll get different results, depending on if you have magic quote on or not.

In any case, relying on magic quotes is a really bad practice. You should:

  1. Disable magic quotes or reverse its effect globally.
  2. Either escape strings when you construct SQL queries or (better) use prepared statements.
  3. Not unescape/strip/whatever anything when you get it back from the database.
我早已燃尽 2024-08-17 23:01:12

即使 magic_quotes_sybase 打开,您可能

function esc($s)
{
    if (get_magic_quotes_gpc()) {
        if (ini_get('magic_quotes_sybase'))
            $s = str_replace("''", "'", $s);
        
        $s = stripslashes($s);
    } //if 
    return mysql_real_escape_string($s);
}

也想去掉斜杠:您可能还想看看 PHP 的 get_magic_quotes_gpc功能页面上,页面上有几个用户评论,其中提供了相当优雅的解决方案,以确保删除斜杠。

You probably want to stripslashes even if magic_quotes_sybase is on:

function esc($s)
{
    if (get_magic_quotes_gpc()) {
        if (ini_get('magic_quotes_sybase'))
            $s = str_replace("''", "'", $s);
        
        $s = stripslashes($s);
    } //if 
    return mysql_real_escape_string($s);
}

You might also want to take a look at PHP's get_magic_quotes_gpc function page, there are several user comments on the page with fairly elegant solutions for ensuring slashes are stripped.

深居我梦 2024-08-17 23:01:12

好的,我已经解决了问题。目前的快速解决方案是,我删除了 function esc($s)
我在 php.ini 中将 Magic_Quote 更改为 OFF。
我保留了addslashes 解决方案。

Ok I have fixed the problem. A quick solution for now, I have removed function esc($s).
I changed Magic_Quote to OFF in php.ini.
I'm keeping addslashes solution.

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