Php 安全电子邮件表单、标题注入查询
我正在使用以下命令清理联系人表单中的输入:Php 安全电子邮件表单、标题注入查询,php,email,header,security,code-injection,Php,Email,Header,Security,Code Injection,我正在使用以下命令清理联系人表单中的输入: <?php $name = strip_tags(stripslashes($_POST['name'])); //this is repeated for several other fields, then: if(isInjected($name)) { die(); } /* see isInjected function below */ // send the mail ?> 我正在使用这个函数: <?php
<?php
$name = strip_tags(stripslashes($_POST['name']));
//this is repeated for several other fields, then:
if(isInjected($name)) { die(); }
/* see isInjected function below */
// send the mail
?>
我正在使用这个函数:
<?php
/* function from http://phpsense.com/php/php-mail.html */
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}
?>
这足以清理我的联系方式吗
谢谢。请注意,代码有点臃肿。它可以很容易地修剪下来:
/* function from http://phpsense.com/php/php-mail.html */
function isInjected($str) {
$inject = "/(\r|\t|%0A|%0D|%08|%09)+/i";
return (preg_match($inject, $str) > 0);
}
它看起来很不错,比一般的输入验证更好。就个人而言,我也更喜欢处理输入类型。在我的basecontroller中,我有几个功能来检查输入是否为有效的出生日期、电子邮件地址等。如果您将此类验证添加到现有验证中,您在我看来处理得很好。为什么围绕
(preg_match($inject,$str)>0)
?:)只是我个人的喜好。它可能源于当我使用三元运算符来确定函数param时,或者在一个合并字符串中,需要使用paren。