阻止php中的邮件注入拦截某些字符\r\n,%0A,%0D
我想阻止人们使用php中用于邮件注入的特殊字符发送垃圾邮件。例如,阻止php中的邮件注入拦截某些字符\r\n,%0A,%0D,php,regex,email,code-injection,spam,Php,Regex,Email,Code Injection,Spam,我想阻止人们使用php中用于邮件注入的特殊字符发送垃圾邮件。例如,\n和\r用于连接多个标题,因此也可与%0A和%0D连接。所以我写了一个正则表达式来匹配它们。但我怀疑我的正则表达式没有那么有效。。。有些东西让我觉得我在写一些无法使用的东西。。。谢谢你的帮助 这里是一个基本的例子,我想做 if (!preg_match('/^[^\s]./', $_POST['name']) || (preg_match('/(%0A|%0D|\\n+|\\r+|;|mime-version:|content-
\n
和\r
用于连接多个标题,因此也可与%0A
和%0D
连接。所以我写了一个正则表达式来匹配它们。但我怀疑我的正则表达式没有那么有效。。。有些东西让我觉得我在写一些无法使用的东西。。。谢谢你的帮助
这里是一个基本的例子,我想做
if (!preg_match('/^[^\s]./', $_POST['name']) || (preg_match('/(%0A|%0D|\\n+|\\r+|;|mime-version:|content-type:|content-transfer-encoding:|subject:|to:|cc:|bcc:)/i', $_POST['name'])) || !(strpos($_POST['name'],'\r') === false) || !(strpos($_POST['name'],'\n') === false)) {
exit("Warning: your Name contains illegal characters! This mail will not be sent!");
} elseif (!preg_match('/^[^\s]./', $_POST['subject']) || (preg_match('/(%0A|%0D|\\n+|\\r+|;|mime-version:|content-type:|content-transfer-encoding:|subject:|to:|cc:|bcc:)/i', $_POST['subject'])) || !(strpos($_POST['subject'],'\r') === false) || !(strpos($_POST['subject'],'\n') === false)) {
exit("Warning: your Subject contains illegal characters! This mail will not be sent!");
} elseif (!preg_match('/^[a-zA-Z0-9]+([_\\.-][a-zA-Z0-9]+)*'.'@([a-zA-Z0-9]+([\.-][a-zA-Z0-9]+))+$/', $_POST['mail'])) {
exit("Warning: your Mail contains no valid email address! This mail will not be sent!");
} elseif (!preg_match('/^[^\s]./', $_POST['message'])) {
exit("Warning: your Message connot be empty! This mail will not be sent!");
} else {
$name = $_POST['name'];
$mail = $_POST['mail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
mail($to, $subject, $message, $headers, "-f$mail") {
exit("Your Mail is sent! Thanks!");
}
为了匹配\n
、\r
、%0A
和%0D
我应该如何编写正则表达式
及
!(strpos($_POST['subject'],'\r')==false)
!(strpos($\u POST['subject'],'\n')==false)
你很好吗
谢谢。我对正则表达式不是很了解,但我可以通过为您的问题提供另一种技巧解决方案来帮助您 您可以使用以下代码来解决您的问题
<?php
function sanitize_strings($raw_string) {
return preg_replace("/[^0-9a-zA-Z_\-\\\%]+/", "", $raw_string);
}
if( $raw_string == sanitize_strings($raw_string) )
{
echo "No illegal characters found.";
}
else
{
echo "Illegal characters found.";
}
?>
希望这对你有用。:) 我做了一个测试。测试结果是成功的!我使用不同的方法直接在
localhost
中测试正则表达式:
<?php
$test = "the dog \n was \r sleeping on the floor";
if (preg_match_all('/(%0A|%0D|\\n+|\\r+|;|mime-version:|content-type:|content-transfer-encoding:|subject:|to:|cc:|bcc:)/i', $test, $tmp)) {
echo "I found this character: '";
print_r($tmp[1]);
echo "'";
} else {
echo "I cannot find any string searched";
}
?>
查看源代码,我可以看到\n和\r
I found this character: 'Array
(
[0] =>
[1] =>
)
'
所以我认为正则表达式构造良好
另外,我还使用了strpos()
进行了其他测试:
查找带双引号的\n时,单引号失败
if !(strpos($_POST['subject'],"\n") === false)) {
结论:正则表达式格式良好,需要“匹配”\n或\r.看看,是关于字段清理的,我正在使用我的正则表达式验证邮件地址:
如果(!preg_match('/^[a-zA-Z0-9]+([\\\.-][a-zA-Z0-9]+.@([a-zA-Z0-9]+([\.-]]a-zA-Z0-9]+)+([\.-]]a-zA-Z0-Z0-9]+))$/”),斜线($$)/”){
谢谢,我需要在我的主题中修复我的正则表达式,我对过滤\u清理\u电子邮件
不感兴趣。如果stripslashes()
意味着你有神奇的引号,你就有一个非常过时的服务器。你可能有比这更糟糕的问题要关心。无论如何,发送电子邮件是很困难的(真的!)这通常不值得你自己去做。只需安装任何第三方库,如Swift Mailer或PhpMailer。-谢谢,我正在运行apache 2.4和php 5.6.17,但由于我有一个脚本,我给其他人,我不知道这些人运行的是现代php版本还是过时版本。我为他们放了条带斜杠t正在运行旧版本的php。因为我不知道我的脚本将在哪里使用。。。
if !(strpos($_POST['subject'],'\n') === false)) {
if !(strpos($_POST['subject'],"\n") === false)) {