PHP 教程
- 第1节:PHP 简介
- 第2节:PHP 安装
- 第3节:PHP 语法
- 第4节:PHP 变量
- 第5节:PHP echo和print 语句
- 第6节:PHP 数据类型
- 第7节:PHP 常量
- 第8节:PHP 字符串变量
- 第9节:PHP 运算符
- 第10节:PHP if...else 语句
- 第11节:PHP switch 语句
- 第12节:PHP 数组
- 第13节:PHP 数组排序
- 第14节:PHP 超级全局变量
- 第15节:PHP while 循环
- 第16节:PHP for 循环
- 第17节:PHP 函数
- 第18节:PHP 魔术常量
- 第19节:PHP 命名空间
- 第20节:PHP 面向对象
- 第21节:PHP 表单
- 第22节:PHP 表单验证
- 第23节:PHP 表单 - 必需字段
- 第24节:PHP 表单 - 验证邮件和URL
- 第25节:PHP 完整表单实例
- 第26节:PHP $_GET 变量
- 第27节:PHP $_POST 变量
- 第28节:PHP 多维数组
- 第29节:PHP 日期
- 第30节:PHP date() 函数
- 第31节:PHP 包含
- 第32节:PHP 文件
- 第33节:PHP 文件上传
- 第34节:PHP Cookies
- 第35节:PHP Sessions
- 第36节:PHP E-Mail
- 第37节:PHP 安全 E-mail
- 第38节:PHP 错误处理
- 第39节:PHP 异常处理
- 第40节:PHP 过滤器
- 第41节:PHP 高级过滤器
- 第42节:PHP JSON
- 第43节:PHP MySQL 简介
- 第44节:PHP 连接 MySQL
- 第45节:PHP MySQL 创建数据库
- 第46节:PHP MySQL 创建数据表
- 第47节:PHP MySQL 插入数据
- 第48节:PHP MySQL 插入多条数据
- 第49节:PHP MySQL 预处理语句
- 第50节:PHP MySQL 读取数据
- 第51节:PHP MySQL Where 子句
- 第52节:PHP MySQL Order By 关键词
- 第53节:PHP MySQL Update
- 第54节:PHP MySQL Delete From
- 第55节:PHP 数据库 ODBC
- 第56节:PHP XML Expat 解析器
- 第57节:PHP XML DOM
- 第58节:PHP XML SimpleXML
- 第59节:PHP AJAX 简介
- 第60节:PHP – AJAX 与 PHP
- 第61节:PHP 实例 AJAX 与 MySQL
- 第62节:PHP 实例 AJAX 与 XML
- 第63节:PHP 实例 AJAX 实时搜索
- 第64节:PHP 实例 AJAX RSS 阅读器
- 第65节:PHP 实例 AJAX 投票
- 第66节:PHP 5 Array 函数
- 第67节:PHP 5 Calendar 函数
- 第68节:PHP cURL 函数
- 第69节:PHP 5 Date/Time 函数
- 第70节:PHP 5 Directory 函数
- 第71节:PHP Error 和 Logging 函数
- 第72节:PHP 5 Filesystem 函数
- 第73节:PHP Filter 函数
- 第74节:PHP FTP 函数
- 第75节:PHP HTTP 函数
- 第76节:PHP Libxml 函数
- 第77节:PHP Mail 函数
- 第78节:PHP 5 Math 函数
- 第79节:PHP Misc 函数
- 第80节:PHP MySQLi 函数
- 第81节:PHP PDO
- 第82节:PHP SimpleXML 函数
- 第83节:PHP String 函数
- 第84节:PHP XML 函数
- 第85节:PHP Zip File 函数
- 第86节:PHP Timezones
- 第87节:PHP 图像处理
- 第88节:PHP 序列化/反序列化函数
- 第89节:PHP 常用header头定义
PHP 安全 E-mail
PHP E-mail 注入
首先,请看上一章中的 PHP 代码:
<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>
以上代码存在的问题是,未经授权的用户可通过输入表单在邮件头部插入数据。
假如用户在表单中的输入框内加入如下文本到电子邮件中,会出现什么情况呢?
someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%0ABTo:person6@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 字段的输入验证程序:
<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>
在上面的代码中,我们使用了 PHP 过滤器来对输入进行验证:
- FILTER_SANITIZE_EMAIL 过滤器从字符串中删除电子邮件的非法字符
- FILTER_VALIDATE_EMAIL 过滤器验证电子邮件地址的值
您可以在我们的 PHP Filter 中阅读更多关于过滤器的知识。
结合本节与上一节的内容,你就可以在 PHP 中安全的发送电子邮件了!