【问题标题】:"I'm" becomes "I\'m" in PHP? (Why is a slash being added?) [duplicate]“我”在 PHP 中变成“我”? (为什么要添加斜线?)[重复]
【发布时间】:2012-03-19 11:15:59
【问题描述】:

可能重复:
Correctly encode characters in a PHP mail form (“I'm” turns to be “I\'m”)

我正在通过 PHP 发送一封电子邮件,并且文本在引号前带有一个斜线:

I'm 变为 I\'m

我的 PHP:

$text_message = $_POST["mymessage"];       


$message="--PHP-mixed-$bound_text\r\n"      
            ."Content-Type: text/plain; charset=\"utf-8\"\r\n"
 ."Content-Transfer-Encoding: 7bit\r\n\r\n" 
       ."$text_message\r\n\r\n" 
    ."--PHP-mixed-$bound_text\r\n"  
            ."Content-Transfer-Encoding: base64\r\n"
            ."Content-Disposition: attachment; filename=\"$attachment\"\r\n"
."Content-Type: image/jpeg; name=\"$attachment\"\r\n\r\n"
 .chunk_split($file)
        ."\r\n\r\n"
            ."--PHP-mixed-$bound_text--\r\n\r\n";
}

如何在不收到额外斜杠的情况下发送它? 谢谢。 乌力

【问题讨论】:

  • @AndrewEllis 那么为什么stripslashes 会起作用呢? (我试图引导答案)。如果它确实有效,那么 $text_message 中的值说明了什么?
  • @pst:如果那不是重复的,那就什么都不是。

标签: php slash


【解决方案1】:

我怀疑是$_POST["mymessage"]。如果你在屏幕上回显$_POST["mymessage"],你会得到什么?

有些网站主机会故意addslashes()$_POST$_GET等收到的数据作为对SQL注入的基本保护。

如果这样做了,你应该可以做到stripslashes($_POST["mymessage"])

【讨论】:

    【解决方案2】:

    你应该检查你的 php.ini 文件中的magic_quotes,它很可能在, 您可以随时在 php 中检查此选项并相应地处理字符串

    if (get_magic_quotes_gpc()){
       $text_message = stripslashes($_POST["mymessage"]);
    }else{
       $text_message = $_POST["mymessage"];
    }
    

    你也应该使用PHP_EOL而不是使用\r\n,然后它与所有操作系统兼容:例如\r对于linux来说不是必需的

    【讨论】:

    • 兴高采烈地退缩了。虽然,因为它是一个比我更好的答案,但我觉得它应该得到支持才能超过我。
    【解决方案3】:

    这是由 PHP 的 magic quotes 引起的,该 magic quotes 已被弃用,但不幸的是,默认情况下仍启用。在大多数情况下,您可以通过.htaccess 文件甚至通过网络托管商的控制面板禁用该功能。

    如果不可能,在盲目使用stripslashes() 之前检查是否通过get_magic_quotes_gpc() 启用了魔术引号是最安全的。要取消转义所有 $_POST[] 变量,请使用:

    if (get_magic_quotes_gpc()) {
        foreach($_POST as $k => $v) {
           $_POST[$k] = stripslashes($v);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 2013-05-05
      • 2023-03-29
      相关资源
      最近更新 更多