检查密码是否满足长度条件
Checking if the password is meeting the length condition
因此,在我将这个新的 if
语句添加到我的代码以检查字符串长度之前,它工作得非常好。我现在尝试添加一个额外的 if
语句来查看表单上的密码长度是否满足 6 到 32 个字符长的条件,但由于某种原因我仍然收到错误(500 服务器错误) .
我试图一遍又一遍地阅读代码,但似乎无法发现导致我出现此错误的原因。我不确定我放置此语句的位置是否可能导致此问题或比此更小更简单的问题。
希望得到一些建议。提前谢谢你。
<?php
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
if($password == $confirmpassword):
$sql = "INSERT INTO users (FirstName, LastName, Role, Email, Username, Password) VALUES (:firstname, :lastname, :role, :email, :username, :password)";
$stmt = $conn->prepare($sql);
$hashPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt->bindParam(':firstname', $_POST['firstname']);
$stmt->bindParam(':lastname', $_POST['lastname']);
$stmt->bindParam(':role', $_POST['role']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', $hashPassword);
if ($stmt->execute()):
echo 'Well done! You have successfully registered with us!';
header('Location:loginPage.php');
else:
echo 'There seems to be an issue getting you registered.';
//$message = 'There seems to be an issue getting you registered.';
endif;
else:
echo 'Your password must be between 6 to 32 characters.';
else:
echo 'Your passwords do not match, please enter them correctly.';
header('Location:registerPage.php');
endif;
endif;
endif;
快速浏览可能的错误原因:
- 第 28 行有一个
else
语句,后跟第 30 行的另一个 else
。
- 第 32 行在回显后有
header(Location:)
调用,这将不起作用,因为 header()
之前不能有任何内容 output/printed。
另外,内部错误(500)通常是Apache配置错误引起的,而不是代码错误。您是否尝试删除多余的 if()
语句并查看您的代码是否有效?
用代码编辑:
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
if($password == $confirmpassword):
$sql = "INSERT INTO users (FirstName, LastName, Role, Email, Username, Password) VALUES (:firstname, :lastname, :role, :email, :username, :password)";
$stmt = $conn->prepare($sql);
$hashPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt->bindParam(':firstname', $_POST['firstname']);
$stmt->bindParam(':lastname', $_POST['lastname']);
$stmt->bindParam(':role', $_POST['role']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', $hashPassword);
if ($stmt->execute()):
echo 'Well done! You have successfully registered with us!';
header('Location:loginPage.php');
else:
echo 'There seems to be an issue getting you registered.';
//$message = 'There seems to be an issue getting you registered.';
endif;
else:
echo 'Your password must be between 6 to 32 characters.';
endif;
// ========= You need to add `endif` (above) before starting another `else`
else:
echo 'Your passwords do not match, please enter them correctly.';
endif;
header('Location:registerPage.php');
endif;
编辑 2,相同的代码但带有额外的可视块行(警告:会产生语法错误):
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
|
| if($password == $confirmpassword):
| | $sql = "INSERT INTO users (FirstName, LastName, Role, Email, Username, Password) VALUES (:firstname, :lastname, :role, :email, :username, :password)";
| | $stmt = $conn->prepare($sql);
| | $hashPassword = password_hash($password, PASSWORD_DEFAULT);
| | $stmt->bindParam(':firstname', $_POST['firstname']);
| | $stmt->bindParam(':lastname', $_POST['lastname']);
| | $stmt->bindParam(':role', $_POST['role']);
| | $stmt->bindParam(':email', $_POST['email']);
| | $stmt->bindParam(':username', $_POST['username']);
| | $stmt->bindParam(':password', $hashPassword);
| | if ($stmt->execute()):
| | echo 'Well done! You have successfully registered with us!';
| | header('Location:loginPage.php');
| | else:
| | echo 'There seems to be an issue getting you registered.';
| | //$message = 'There seems to be an issue getting you registered.';
| | endif;
| else:
| echo 'Your password must be between 6 to 32 characters.';
| endif;
|
// ========= You need to add `endif` (above) before starting another `else`
else:
echo 'Your passwords do not match, please enter them correctly.';
endif;
header('Location:registerPage.php');
endif;
因此,在我将这个新的 if
语句添加到我的代码以检查字符串长度之前,它工作得非常好。我现在尝试添加一个额外的 if
语句来查看表单上的密码长度是否满足 6 到 32 个字符长的条件,但由于某种原因我仍然收到错误(500 服务器错误) .
我试图一遍又一遍地阅读代码,但似乎无法发现导致我出现此错误的原因。我不确定我放置此语句的位置是否可能导致此问题或比此更小更简单的问题。
希望得到一些建议。提前谢谢你。
<?php
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
if($password == $confirmpassword):
$sql = "INSERT INTO users (FirstName, LastName, Role, Email, Username, Password) VALUES (:firstname, :lastname, :role, :email, :username, :password)";
$stmt = $conn->prepare($sql);
$hashPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt->bindParam(':firstname', $_POST['firstname']);
$stmt->bindParam(':lastname', $_POST['lastname']);
$stmt->bindParam(':role', $_POST['role']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', $hashPassword);
if ($stmt->execute()):
echo 'Well done! You have successfully registered with us!';
header('Location:loginPage.php');
else:
echo 'There seems to be an issue getting you registered.';
//$message = 'There seems to be an issue getting you registered.';
endif;
else:
echo 'Your password must be between 6 to 32 characters.';
else:
echo 'Your passwords do not match, please enter them correctly.';
header('Location:registerPage.php');
endif;
endif;
endif;
快速浏览可能的错误原因:
- 第 28 行有一个
else
语句,后跟第 30 行的另一个else
。 - 第 32 行在回显后有
header(Location:)
调用,这将不起作用,因为header()
之前不能有任何内容 output/printed。
另外,内部错误(500)通常是Apache配置错误引起的,而不是代码错误。您是否尝试删除多余的 if()
语句并查看您的代码是否有效?
用代码编辑:
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
if($password == $confirmpassword):
$sql = "INSERT INTO users (FirstName, LastName, Role, Email, Username, Password) VALUES (:firstname, :lastname, :role, :email, :username, :password)";
$stmt = $conn->prepare($sql);
$hashPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt->bindParam(':firstname', $_POST['firstname']);
$stmt->bindParam(':lastname', $_POST['lastname']);
$stmt->bindParam(':role', $_POST['role']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':password', $hashPassword);
if ($stmt->execute()):
echo 'Well done! You have successfully registered with us!';
header('Location:loginPage.php');
else:
echo 'There seems to be an issue getting you registered.';
//$message = 'There seems to be an issue getting you registered.';
endif;
else:
echo 'Your password must be between 6 to 32 characters.';
endif;
// ========= You need to add `endif` (above) before starting another `else`
else:
echo 'Your passwords do not match, please enter them correctly.';
endif;
header('Location:registerPage.php');
endif;
编辑 2,相同的代码但带有额外的可视块行(警告:会产生语法错误):
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
require 'connection.php';
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
if (!empty($_POST['username']) && !empty($_POST['password'])):
//var_dump($password, $confirmpassword);
if(strlen($password) < 6 || strlen($password) > 32):
|
| if($password == $confirmpassword):
| | $sql = "INSERT INTO users (FirstName, LastName, Role, Email, Username, Password) VALUES (:firstname, :lastname, :role, :email, :username, :password)";
| | $stmt = $conn->prepare($sql);
| | $hashPassword = password_hash($password, PASSWORD_DEFAULT);
| | $stmt->bindParam(':firstname', $_POST['firstname']);
| | $stmt->bindParam(':lastname', $_POST['lastname']);
| | $stmt->bindParam(':role', $_POST['role']);
| | $stmt->bindParam(':email', $_POST['email']);
| | $stmt->bindParam(':username', $_POST['username']);
| | $stmt->bindParam(':password', $hashPassword);
| | if ($stmt->execute()):
| | echo 'Well done! You have successfully registered with us!';
| | header('Location:loginPage.php');
| | else:
| | echo 'There seems to be an issue getting you registered.';
| | //$message = 'There seems to be an issue getting you registered.';
| | endif;
| else:
| echo 'Your password must be between 6 to 32 characters.';
| endif;
|
// ========= You need to add `endif` (above) before starting another `else`
else:
echo 'Your passwords do not match, please enter them correctly.';
endif;
header('Location:registerPage.php');
endif;