admin 发表于 2023-2-16 18:54:46

PHP Secure E-mails

<div id="article_content" class="article_content clearfix">
      <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
      <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css">
                <div id="content_views" class="htmledit_views">
                  <h2>PHP E-mail 注入</h2>
<p>首先&#xff0c;请看上一章中的 PHP 代码&#xff1a;</p>
<pre><html>
<head>
<meta charset&#61;&#34;utf-8&#34;>
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<?php
if (isset($_REQUEST['email'])) { // 如果接收到邮箱参数则发送邮件
    // 发送邮件
    $email &#61; $_REQUEST['email'] ;
    $subject &#61; $_REQUEST['subject'] ;
    $message &#61; $_REQUEST['message'] ;
    mail(&#34;someone&#64;example.com&#34;, $subject,
    $message, &#34;From:&#34; . $email);
    echo &#34;邮件发送成功&#34;;
} else { // 如果没有邮箱参数则显示表单
    echo &#34;<form method&#61;'post' action&#61;'mailform.php'>
    Email: <input name&#61;'email' type&#61;'text'><br>
    Subject: <input name&#61;'subject' type&#61;'text'><br>
    Message:<br>
    <textarea name&#61;'message' rows&#61;'15' cols&#61;'40'>
    </textarea><br>
    <input type&#61;'submit'>
    </form>&#34;;
}
?>

</body>
</html></pre>
<p>以上代码存在的问题是&#xff0c;未经授权的用户可通过输入表单在邮件头部插入数据。</p>
<p>假如用户在表单中的输入框内加入如下文本到电子邮件中&#xff0c;会出现什么情况呢&#xff1f;</p>
<pre>someone&#64;example.com%0ACc:person2&#64;example.com
%0ABcc:person3&#64;example.com,person3&#64;example.com,
anotherperson4&#64;example.com,person5&#64;example.com
%0ABTo:person6&#64;example.com</pre>
<p>与往常一样&#xff0c;mail() 函数把上面的文本放入邮件头部&#xff0c;那么现在头部有了额外的 Cc:、Bcc: 和 To: 字段。当用户点击提交按钮时&#xff0c;这封 e-mail 会被发送到上面所有的地址&#xff01;</p>
<hr>
<h2>PHP 防止 E-mail 注入</h2>
<p>防止 e-mail 注入的最好方法是对输入进行验证。</p>
<p>下面的代码与上一章中的类似&#xff0c;不过这里我们已经增加了检测表单中 email 字段的输入验证程序&#xff1a;</p>
<pre><html>
<head>
<meta charset&#61;&#34;utf-8&#34;>
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<?php
function spamcheck($field)
{
    // filter_var() 过滤 e-mail
    // 使用 FILTER_SANITIZE_EMAIL
    $field&#61;filter_var($field, FILTER_SANITIZE_EMAIL);

    //filter_var() 过滤 e-mail
    // 使用 FILTER_VALIDATE_EMAIL
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

if (isset($_REQUEST['email']))
{
    // 如果接收到邮箱参数则发送邮件

    // 判断邮箱是否合法
    $mailcheck &#61; spamcheck($_REQUEST['email']);
    if ($mailcheck&#61;&#61;FALSE)
    {
        echo &#34;非法输入&#34;;
    }
    else
    {    
        // 发送邮件
        $email &#61; $_REQUEST['email'] ;
        $subject &#61; $_REQUEST['subject'] ;
        $message &#61; $_REQUEST['message'] ;
        mail(&#34;someone&#64;example.com&#34;, &#34;Subject: $subject&#34;,
        $message, &#34;From: $email&#34; );
        echo &#34;Thank you for using our mail form&#34;;
    }
}
else
{
    // 如果没有邮箱参数则显示表单
    echo &#34;<form method&#61;'post' action&#61;'mailform.php'>
    Email: <input name&#61;'email' type&#61;'text'><br>
    Subject: <input name&#61;'subject' type&#61;'text'><br>
    Message:<br>
    <textarea name&#61;'message' rows&#61;'15' cols&#61;'40'>
    </textarea><br>
    <input type&#61;'submit'>
    </form>&#34;;
}
?>

</body>
</html></pre>
<p>在上面的代码中&#xff0c;我们使用了 PHP 过滤器来对输入进行验证&#xff1a;</p>
<ul><li>FILTER_SANITIZE_EMAIL 过滤器从字符串中删除电子邮件的非法字符</li><li>FILTER_VALIDATE_EMAIL 过滤器验证电子邮件地址的值</li></ul>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: PHP Secure E-mails