Php 筛选\验证\电子邮件不工作
我肯定错过了什么。由于某些原因,筛选器_var不工作。我正在尝试验证来自$\u POST的电子邮件,即使有有效的电子邮件,它也会返回false。但是,当我硬编码一封电子邮件时,它工作得很好。怎么了 以下是我的php代码:Php 筛选\验证\电子邮件不工作,php,validation,email-validation,filter-var,Php,Validation,Email Validation,Filter Var,我肯定错过了什么。由于某些原因,筛选器_var不工作。我正在尝试验证来自$\u POST的电子邮件,即使有有效的电子邮件,它也会返回false。但是,当我硬编码一封电子邮件时,它工作得很好。怎么了 以下是我的php代码: function redirect() { //redirecting to home page function. Used in one of the lectures. $host = $_SERVER["HTTP_HOST"]; $path = rtr
function redirect() { //redirecting to home page function. Used in one of the lectures.
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/index.php");
exit;
}
try
{
$dbh = new PDO($db, $dbuser, $dbpassword);
}
catch (PDOException $e)
{
echo "Connection failure: " . $e->getmessage();
}
if (!isset($_POST['email']) || !isset($_POST['password1']) || !isset($_POST['password2'])) {
redirect();
}
$password1 = htmlspecialchars($_POST['password1']);
$email = htmlspecialchars($_POST['email']);
$password2 = htmlspecialchars($_POST['password2']);
//preg_match('/.+@.+\./', $email) == FALSE
if ($email = "") {
print "email not there";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
print "not real email";
} elseif (strlen($password1) < 6) {
print("password too small");
} elseif (!(preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $password1))) {
print "numbers and letters plz";
} elseif ($password1 != $password2) {
print "passwords not same";
//redirect();
}
function redirect(){//重定向到主页函数。用于其中一节课。
$host=$\u服务器[“HTTP\u主机”];
$path=rtrim(dirname($\u SERVER[“PHP\u SELF”]),“/\”;
标题(“位置:http://$host$path/index.php”);
出口
}
尝试
{
$dbh=新PDO($db,$dbuser,$dbpassword);
}
捕获(PDO$e)
{
echo“连接失败:”..e->getmessage();
}
如果(!isset($_POST['email'])| |!isset($_POST['password1'])| |!isset($_POST['password2'])){
重定向();
}
$password1=htmlspecialchars($_POST['password1']);
$email=htmlspecialchars($_POST['email']);
$password2=htmlspecialchars($_POST['password2']);
//preg_match('/.+@.+\./',$email)=FALSE
如果($email=”“){
打印“电子邮件不在那里”;
}elseif(!filter\u var($email,filter\u VALIDATE\u email)){
打印“非真实电子邮件”;
}elseif(strlen($password1)<6){
打印(“密码太小”);
}其他(!(预匹配('/[A-Za-z].[0-9].[0-9].[A-Za-z]/',$password1))){
打印“数字和字母plz”;
}elseif($password1!=$password2){
打印“密码不相同”;
//重定向();
}
更改第一次电子邮件检查:
if ($email == "") {
print "email not there";
}
它得到的是
“
值,而不是检查值。没有htmlspecialchars()
,它能工作吗?天哪!我觉得自己真的很笨。非常感谢。为什么没有抛出任何错误?@undacovabroda45,你不应该像这样做空检查,请使用if(empty($email))
@undacovabroda45这不是错误。您正在$email
中插入一个值。