课程表

PHP 基础教程

PHP 表单

PHP 高级教程

PHP 数据库

PHP XML

PHP 和 AJAX

PHP 参考手册

PHP 案例

工具箱
速查手册

PHP 安全 E-mail

当前位置:免费教程 » 程序设计 » PHP

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

PHP E-mail 注入

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

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

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

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

  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 字段的输入验证程序:

  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.  
  21. if (isset($_REQUEST['email']))
  22. {//if "email" is filled out, proceed
  23.  
  24. //check if the email address is invalid
  25. $mailcheck = spamcheck($_REQUEST['email']);
  26. if ($mailcheck==FALSE)
  27. {
  28. echo "Invalid input";
  29. }
  30. else
  31. {//send email
  32. $email = $_REQUEST['email'] ;
  33. $subject = $_REQUEST['subject'] ;
  34. $message = $_REQUEST['message'] ;
  35. mail("someone@example.com", "Subject: $subject",
  36. $message, "From: $email" );
  37. echo "Thank you for using our mail form";
  38. }
  39. }
  40. else
  41. {//if "email" is not filled out, display the form
  42. echo "<form method='post' action='mailform.php'>
  43. Email: <input name='email' type='text' /><br />
  44. Subject: <input name='subject' type='text' /><br />
  45. Message:<br />
  46. <textarea name='message' rows='15' cols='40'>
  47. </textarea><br />
  48. <input type='submit' />
  49. </form>";
  50. }
  51. ?>
  52.  
  53. </body>
  54. </html>

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

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

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

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号