检查密码是否满足长度条件


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()因为它之前不能有任何输出/打印。

此外,通常内部错误(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;