PHP Basic

PHP HOME
PHP Intro
PHP Install
PHP Syntax
PHP Variables
PHP String
PHP Operators
PHP If...Else
PHP Switch
PHP Arrays
PHP While Loops
PHP For Loops
PHP Functions
PHP Forms
PHP $_GET
PHP $_POST

PHP Advanced

PHP Date
PHP Include
PHP File
PHP File Upload
PHP Cookies
PHP Sessions
PHP E-mail
PHP Secure E-mail
PHP Error
PHP Exception
PHP Filter

PHP Database

MySQL Introduction
MySQL Connect
MySQL Create
MySQL Insert
MySQL Select
MySQL Where
MySQL Order By
MySQL Update
MySQL Delete
PHP ODBC

PHP XML

XML Expat Parser
XML DOM
XML SimpleXML

PHP and AJAX

AJAX Intro
AJAX PHP
AJAX Database
AJAX XML
AJAX Live Search
AJAX RSS Reader
AJAX Poll

PHP Reference

PHP Array
PHP Calendar
PHP Date
PHP Directory
PHP Error
PHP Filesystem
PHP Filter
PHP FTP
PHP HTTP
PHP Libxml
PHP Mail
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip

PHP Quiz

PHP Quiz
PHP Certificate

PHP Secure E-mails

« Previous Next Chapter »

There is a weakness in the PHP e-mail script in the previous chapter.


PHP E-mail Injections

First, look at the PHP code from the previous chapter:

<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
   {
   //send email
   $email = $_REQUEST['email'] ;
   $subject = $_REQUEST['subject'] ;
   $message = $_REQUEST['message'] ;
   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='post' action='mailform.php'>
   Email: <input name='email' type='text' /><br />
   Subject: <input name='subject' type='text' /><br />
   Message:<br />
   <textarea name='message' rows='15' cols='40'>
   </textarea><br />
   <input type='submit' />
   </form>";
   }
?>

</body>
</html>

The problem with the code above is that unauthorized users can insert data into the mail headers via the input form.

What happens if the user adds the following text to the email input field in the form?

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

The mail() function puts the text above into the mail headers as usual, and now the header has an extra Cc:, Bcc:, and To: field. When the user clicks the submit button, the e-mail will be sent to all of the addresses above!


PHP Stopping E-mail Injections

The best way to stop e-mail injections is to validate the input.

The code below is the same as in the previous chapter, but now we have added an input validator that checks the email field in the form:

<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['email']))
   {//if "email" is filled out, proceed

   //check if the email address is invalid
   $mailcheck = spamcheck($_REQUEST['email']);
   if ($mailcheck==FALSE)
     {
     echo "Invalid input";
     }
   else
     {//send email
     $email = $_REQUEST['email'] ;
     $subject = $_REQUEST['subject'] ;
     $message = $_REQUEST['message'] ;
     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='post' action='mailform.php'>
   Email: <input name='email' type='text' /><br />
   Subject: <input name='subject' type='text' /><br />
   Message:<br />
   <textarea name='message' rows='15' cols='40'>
   </textarea><br />
   <input type='submit' />
   </form>";
   }
?>

</body>
</html>

In the code above we use PHP filters to validate input:

You can read more about filters in our PHP Filter chapter.


« Previous Next Chapter »


宏飞网络是你学习web开发、测试web程序实例、和培养职业技能的首选网站。我们提供例子也许有些简单,但对理解基本概念有帮助。

我们尽量避免在教程、参考及例子中出现错误,但不能保证所有的内容都是正确的。

你使用本网站时,我们默认你已经阅读并接受了我们的隐私政策。

Copyright 2003-2011宏飞网络 版权所有