PHP 安全的電子郵件

巴扎黑
發布: 2016-11-11 18:03:48
原創
1422 人瀏覽過


PHP 安全的電子郵件

PHP E-mail

PHP Error

在上一節中的 PHP e-mail 腳本中,存在著一個漏洞。

PHP E-mail 注入

首先,請看上一節中的 PHP 代碼:

<html>
<body>
<?php
if (isset($_REQUEST[&#39;email&#39;]))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST[&#39;email&#39;] ; 
  $subject = $_REQUEST[&#39;subject&#39;] ;
  $message = $_REQUEST[&#39;message&#39;] ;
  mail("someone@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method=&#39;post&#39; action=&#39;mailform.php&#39;>
  Email: <input name=&#39;email&#39; type=&#39;text&#39; /><br />
  Subject: <input name=&#39;subject&#39; type=&#39;text&#39; /><br />
  Message:<br />
  <textarea name=&#39;message&#39; rows=&#39;15&#39; cols=&#39;40&#39;>
  </textarea><br />
  <input type=&#39;submit&#39; />
  </form>";
  }
?>
</body>
</html>
登入後複製

以上代碼存在的問題是,未經授權的用戶可透過輸入表單在郵件頭部插入資料。

假如使用者在表單中的輸入框內加入這些文本,會出現什麼情況呢?

someone@example.com%0ACc:person2@example.com

%0ABcc:person3@example.com,person3@example.com,

anotherperson4@example.com@example.com,

anotherperson4@example.com,person5:exle.com person6@example.com

與往常一樣,mail() 函數把上面的文字放入郵件頭部,那麼現在頭部有了額外的Cc:, Bcc: 以及To: 字段。當使用者點擊提交按鈕時,這封 e-mail 會被送到上面所有的地址!

PHP 防止 E-mail 注入

防止 e-mail 注入的最佳方法是對輸入進行驗證。

下面的程式碼與上一節類似,不過我們已經增加了檢測表單中email 欄位的輸入驗證程序:

<html>
<body>
<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail 
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  
  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }
if (isset($_REQUEST[&#39;email&#39;]))
  {//if "email" is filled out, proceed
  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST[&#39;email&#39;]);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    }
  else
    {//send email
    $email = $_REQUEST[&#39;email&#39;] ; 
    $subject = $_REQUEST[&#39;subject&#39;] ;
    $message = $_REQUEST[&#39;message&#39;] ;
    mail("someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  }
else
  {//if "email" is not filled out, display the form
  echo "<form method=&#39;post&#39; action=&#39;mailform.php&#39;>
  Email: <input name=&#39;email&#39; type=&#39;text&#39; /><br />
  Subject: <input name=&#39;subject&#39; type=&#39;text&#39; /><br />
  Message:<br />
  <textarea name=&#39;message&#39; rows=&#39;15&#39; cols=&#39;40&#39;>
  </textarea><br />
  <input type=&#39;submit&#39; />
  </form>";
  }
?>
</body>
</html>
登入後複製

在上面的程式碼中,我們使用了PHP 過濾器來驗證輸入:

FILTER_SANITIZE_EMAIL從字串中刪除電子郵件的非法字元

FILTER_VALIDATE_EMAIL 驗證電子郵件地址



來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!