关于php:500错误与password_verify()

500 Error with password_verify()

本问题已经有最佳答案,请猛点这里访问。

出于某些我自己不知道的原因,当我尝试用这个登录脚本加载一个页面时,我收到一个500错误。我知道这与if语句有关,但我不确定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php  //Start the Session
session_start();
require('connect.php');
if (isset($_POST) & !empty($_POST)){
  $username = $_POST['username'];
  $password = $_POST['password'];

   $sql = $connection->prepare("SELECT * FROM login WHERE username=username");
   $sql->bindParam('username', $username);
   $sql->execute();
    if(($row = $sql->fetch()) && (password_verify($$password,$row['password']))){

              echo"hurray, you authenticated.<br/>";
          }
          else {
              //header("Location:../../login/login.p
  echo"invalid login<br/>";
}

?>

这是供参考的注册脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
require_once('connect.php');
if(isset($_POST) && !empty($_POST)){
    $username = mysqli_real_escape_string($connection, $_POST['username']);
    $email = mysqli_real_escape_string($connection, $_POST['email']);
    $password =$_POST['password'];
    $password = password_hash($password,PASSWORD_BCRYPT);


    $sql = $connection->prepare("INSERT INTO `login` (username, email, password) VALUES (?, ?, ?)");
    $sql->bind_param("sss", $username, $email, $password);
    $sql->execute();
    if($sql){
        echo"User Rego Secussefull";
    }else{
        echo"User rego failed";
    }
 }

?>


如果$$password返回变量密码的名称,您可以将准备状态更改为username=:username并检查您的行。请参阅此处的详细信息(https://secure.php.net/manual/pt_br/language.variables.variable.php)

您可以更改为简化您的第一个if,以:

1
if (isset($_POST['username']) && isset($_POST['password']) {

1
if (empty($_POST) { // $_POST is global setted by default, but empty/null