您现在的位置: 365建站网 > 建站教程 > PHP 教程 > PHP 安全的电子邮件

PHP 安全的电子邮件

此节有 294 人学习过     参与评论

在上一节中的 PHP e-mail 脚本中,存在着一个漏洞。

PHP E-mail 注入

首先,请看上一节中的 PHP 代码:

</>code

  1. <html>
  2. <body>
  3. <?php
  4. if (isset($_REQUEST['email']))
  5. //if "email" is filled out, send email
  6. {
  7. //send email
  8. $email = $_REQUEST['email'] ;
  9. $subject = $_REQUEST['subject'] ;
  10. $message = $_REQUEST['message'] ;
  11. mail("someone@example.com", "Subject: $subject",
  12. $message, "From: $email" );
  13. echo "Thank you for using our mail form";
  14. }
  15. else
  16. //if "email" is not filled out, display the form
  17. {
  18. echo "<form method='post' action='mailform.php'>
  19. Email: <input name='email' type='text' /><br />
  20. Subject: <input name='subject' type='text' /><br />
  21. Message:<br />
  22. <textarea name='message' rows='15' cols='40'>
  23. </textarea><br />
  24. <input type='submit' />
  25. </form>";
  26. }
  27. ?>
  28. </body>
  29. </html>

以上代码存在的问题是,未经授权的用户可通过输入表单在邮件头部插入数据。

假如用户在表单中的输入框内加入这些文本,会出现什么情况呢?

</>code

  1. someone@example.com%0ACc:person2@example.com
  2. %0ABcc:person3@example.com,person3@example.com,
  3. anotherperson4@example.com,person5@example.com
  4. %0ABTo:person6@example.com

与往常一样,mail() 函数把上面的文本放入邮件头部,那么现在头部有了额外的 Cc:, Bcc: 以及 To: 字段。当用户点击提交按钮时,这封 e-mail 会被发送到上面所有的地址!

PHP 防止 E-mail 注入

防止 e-mail 注入的最好方法是对输入进行验证。

下面的代码与上一节类似,不过我们已经增加了检测表单中 email 字段的输入验证程序:

</>code

  1. <html>
  2. <body>
  3. <?php
  4. function spamcheck($field)
  5. {
  6. //filter_var() sanitizes the e-mail
  7. //address using FILTER_SANITIZE_EMAIL
  8. $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  9. //filter_var() validates the e-mail
  10. //address using FILTER_VALIDATE_EMAIL
  11. if(filter_var($field, FILTER_VALIDATE_EMAIL))
  12. {
  13. return TRUE;
  14. }
  15. else
  16. {
  17. return FALSE;
  18. }
  19. }
  20. if (isset($_REQUEST['email']))
  21. {//if "email" is filled out, proceed
  22. //check if the email address is invalid
  23. $mailcheck = spamcheck($_REQUEST['email']);
  24. if ($mailcheck==FALSE)
  25. {
  26. echo "Invalid input";
  27. }
  28. else
  29. {//send email
  30. $email = $_REQUEST['email'] ;
  31. $subject = $_REQUEST['subject'] ;
  32. $message = $_REQUEST['message'] ;
  33. mail("someone@example.com", "Subject: $subject",
  34. $message, "From: $email" );
  35. echo "Thank you for using our mail form";
  36. }
  37. }
  38. else
  39. {//if "email" is not filled out, display the form
  40. echo "<form method='post' action='mailform.php'>
  41. Email: <input name='email' type='text' /><br />
  42. Subject: <input name='subject' type='text' /><br />
  43. Message:<br />
  44. <textarea name='message' rows='15' cols='40'>
  45. </textarea><br />
  46. <input type='submit' />
  47. </form>";
  48. }
  49. ?>
  50. </body>
  51. </html>

在上面的代码中,我们使用了 PHP 过滤器来对输入进行验证:

  • FILTER_SANITIZE_EMAIL 从字符串中删除电子邮件的非法字符
  • FILTER_VALIDATE_EMAIL 验证电子邮件地址

您可以在我们的 PHP 过滤器这一节中阅读更多有关过滤器的内容。

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (294人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------
PHP 教程目录

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号