PHP正则表达式试图检测小于和大于字符时出现问题
我有一个简单的表单,要求输入用户名和密码。正则表达式应检测一个或多个小于或大于字符。PHP正则表达式试图检测小于和大于字符时出现问题,php,regex,Php,Regex,我有一个简单的表单,要求输入用户名和密码。正则表达式应检测一个或多个小于或大于字符。 大于时,所有功能都正常工作,但小于时,会导致错误反馈消息的切断/停止 例如: 如果我输入'road'作为用户名,输入'w>ay'作为密码,我会得到: road is good to go Your entry: "w>ay" contains illegal characters. Please re-enter, and be sure the < or the > are not pres
大于时,所有功能都正常工作,但小于时,会导致错误反馈消息的切断/停止 例如: 如果我输入'road'作为用户名,输入'w>ay'作为密码,我会得到:
road is good to go
Your entry: "w>ay" contains illegal characters.
Please re-enter, and be sure the < or the > are not present.
这条路很好走
您的条目:“w>ay”包含非法字符。
请重新输入,并确保<或>不存在。
现在,如果我输入
'w您需要使用htmlspecialchars:
htmlspecialchars($dataEntered, ENT_QUOTES);
您为什么禁止他们在密码中使用
?您正在尝试停止HTML注入吗?不是这样做的。不,这只是一个练习要求,这两个特定的字符不能被接受。这里有一点奇怪…我问题的最后一行应该是:现在,如果我输入“wWouldn't let me edit That…”hmmmmhtmlspecialchars
更安全,允许htmlentities
这样做是一个巨大的风险。非常感谢Ed Cottrell和l'l
<?php
function characterEntryCheck($dataEntered)
{
$illegal="/[><]+/";
if(preg_match($illegal, $dataEntered))
{
echo "Your entry: \"" . $dataEntered . "\" contains illegal characters." . "<br>";
echo "Please re-enter, and be sure the < or the > are not present." ."<br>";
exit;
}
else{
echo $dataEntered . " is good to go" . "<br>"; // feedback for testing only
}
}
/* Variable passed from input data fields in form */
$name=$_POST["u"];
$password=$_POST["p"];
if(!empty($name)&&!empty($password)) // has data has been entered?
{
characterEntryCheck($name);
characterEntryCheck($password);
}else{
echo "Please enter data in all of the fields"; // feedback for testing only
}
htmlspecialchars($dataEntered, ENT_QUOTES);
echo "Your entry: \"" . htmlspecialchars($dataEntered) . "\" contains illegal characters." . "<br>";