我正在尝试在我的注册页面上使用电子邮件验证,但即使是合法的电子邮件地址,if(filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL) !== false)
也始终返回false,例如:bob.smith@hotmail.com
PHP代码:
<?php require 'Assets/Connections/Connections.php';
session_start();
if(isset($_POST['Register']))
{
//whether the firstname is blank
if ($_POST['First_Name'] == '')
{
$_SESSION['First_NameErr'] = "First Name is required.";
}
//whether the lastname is blank
if ($_POST['Last_Name'] == '')
{
$_SESSION['Last_NameErr'] = "Last Name is required.";
}
//whether the email is blank
if($_POST['Email'] == '')
{
$_SESSION['EmailErr1'] = "E-mail is required.";
}
else
{
//whether the email format is correct
if(filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL) !== false)
{
// Return Success - Valid Email
$Email = mysqli_real_escape_string($con, $_POST['Email']);
$sql1 = "SELECT * FROM user
WHERE email = '$Email'";
$result1 = mysqli_query($con, $sql1)
or die(mysqli_error());
if (mysqli_num_rows($result1) > 0)
{
$_SESSION['EmailErr2'] = "This Email is already used.";
}
}
else
{
// Return Error - Invalid Email
$_SESSION['EmailErr3'] = "Your email is not valid.";
}
}
//whether the password is blank
if($_POST['Password'] == '')
{
$_SESSION['PasswordErr'] = "Password is required.";
}
//if an error is created, the user is re-directed to a blank registration form
if(isset($_SESSION['First_NameErr']) || isset($_SESSION['Last_NameErr']) || isset($_SESSION['EmailErr1']) || isset($_SESSION['EmailErr2']) || isset($_SESSION['EmailErr3']) || isset($_SESSION['PasswordErr']))
{
echo "Error";
var_dump(filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL));
#header("Location: Register.php");
#exit;
}
else
{
$FName = mysqli_real_escape_string($con, $_POST['First_Name']);
$LName = mysqli_real_escape_string($con, $_POST['Last_Name']);
$Email = mysqli_real_escape_string($con, $_POST['Email']);
$PW = mysqli_real_escape_string($con, $_POST['Password']);
$StorePassword = password_hash($PW, PASSWORD_BCRYPT, array('cost' => 10));
$sql = $con->query("INSERT INTO user (Fname, Lname, Email, Password)
Values('$FName', '$LName', '$Email', '$StorePassword')")
or die(mysqli_error());
header('Location: LogIn.php');
die();
}
}
?>
Html表格:
<!doctype html>
<html>
<head>
<link href="Assets/CSS/Master.css" rel="stylesheet" type="text/css" />
<link href="Assets/CSS/Menu.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Register</title>
</head>
<body>
<div class="Container">
<div class="Header"></div>
<div class="Menu">
<div id="Menu">
<nav>
<ul class="cssmenu">
<li><a href="FrontPage.php">Home</a></li>
<li><a href="Register.php">Register</a></li>
<li><a href="LogIn.php">LogIn</a></li>
</ul>
</nav>
</div>
</div>
<div class="CentreBody">
<form id="RegisterForm" name="RegisterForm" method="post">
<div class="FormElement">
<input type="text" name="First_Name" required="required" class="TField" id="First_Name" placeholder="First Name">
</div>
<div class="FormElement">
<input type="text" name="Last_Name" required="required" class="TField" id="Last_Name" placeholder="Last Name">
</div>
<div class="FormElement">
<input type="email" name="Email" required="required" class="TField" id="Email" placeholder="Email">
</div>
<div class="FormElement">
<input type="password" name="Password" required="required" class="TField" id="Password" placeholder="Password">
</div>
<div class="FormElement">
<input type="submit" name="Register" class="button" id="Register" value="Register">
</div>
</form>
</div>
<div class="Footer"></div>
</div>
</body>
</html>
答案 0 :(得分:0)
我改变了第一行。 我还将修剪功能添加到$ _POST [&#39;电子邮件&#39;] $ _POST [&#39; Email&#39;] = trim($ _ POST [&#39; Email&#39;]);
电子邮件的验证是好的,唯一可能失败的方法是电子邮件是否有空间。喜欢&#34; bob.smith@hotmail.com&lt;空间&gt;&#34;
我希望这可以提供帮助!
<?php
#if($_POST['Email'] == ''){
if(!empty($_POST['Email']) && isset($_POST['Email'])){
$_SESSION['EmailErr1'] = "E-mail is required.";
}else{
//whether the email format is correct
$_POST['Email'] = trim($_POST['Email']);
if(filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL) !== False){
$Email = mysqli_real_escape_string($con, $_POST['Email']);
$sql1 = "SELECT * FROM user WHERE email = '$Email'";
$result1 = mysqli_query($con, $sql1) or die(mysqli_error());
if (mysqli_num_rows($result1) > 0){
$_SESSION['EmailErr2'] = "This Email is already used.";
}
}else{
// Return Error - Invalid Email
$_SESSION['EmailErr3'] = "Your email is not valid.";
}
}
//if an error is created, the user is re-directed to a blank registration form
if(isset($_SESSION['EmailErr1']) || isset($_SESSION['EmailErr2']) || isset($_SESSION['EmailErr3'])){
echo "Error";
var_dump(filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL));
}else{
$Email = mysqli_real_escape_string($con, $_POST['Email']);
INSERT into table....
}
?>