小白教程

 找回密码
 立即注册
小白教程 首页 系列教程 PHP系列教程 查看内容

PHP Secure E-mails

发布者: 小白教程

我们在上一节内容中已经介绍过 PHP 发送电子邮件的方式了,但是在上一节中的 PHP e-mail 脚本中,存在着一个漏洞,接下来我们一起来解决这个漏洞!


PHP E-mail 注入

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

  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>

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

假如用户在表单中的输入框内加入如下文本到电子邮件中,会出现什么情况呢?

someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%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. 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 过滤器验证电子邮件地址的值

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-19 12:16 , Processed in 0.023103 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

返回顶部