注意:只有变量应该通过引用传递

7
以下是我的PHP代码。
<?php
session_start();

if(isset($_SESSION['user_id'])) {
    header("Location: /");
}

require 'database.php';

$message = '';

    if(!empty($_POST['email']) && !empty($_POST['password'])):
        //Enter the new user in the database.
        $sql ="INSERT INTO users (email, password) VALUES(:email, :password)";
        $stmt = $conn->prepare($sql);

        $stmt->bindParam(':email', $_POST['email']);
        $stmt->bindParam(':password',password_hash($_POST['password'], PASSWORD_BCRYPT));
        if ($stmt->execute()):
            $message = 'successfully created new user';
        else:
            $message = 'Sorry there must have been an issue creating your an account.';
        endif;
    endif;

它显示了一个错误,内容是:

注意:C:\xampp\htdocs\auth\register.php的第17行只应传递变量作为引用。

在第17行,有以下代码:

$stmt->bindParam(':password',password_hash($_POST['password'], PASSWORD_BCRYPT));

你知道问题是什么,Only variables should be passed by reference 是什么意思吗?

3个回答

11

bind_param 通过引用传递值。它会查看您传递的变量,并直接指向内部。

在您的调用中,您返回了函数调用的字符串结果 - 在本例中为 password_hash。因为没有涉及到变量,所以没有内部可以进行指向。PHP因此无法将数据作为结果按引用传递而发出警告。

您需要将函数调用的结果放入变量中,然后将该变量传递给 bind

请尝试以下操作:

$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$stmt->bindParam(':password',$password );

参考来源:这里


这可能并不完全是在“抱怨”,我发现添加这一步骤还可以防止与字符串变量中的引号相关的某些问题。 - Jeremy Young

5
    $hashedpassword = password_hash($_POST['password'], PASSWORD_BCRYPT);
    $stmt->bindParam(':password',$hashedpassword );

-1

首先创建

$password = password_hash($_POST['password'], PASSWORD_BCRYPT);

然后这个

$stmt->bindParam(':password',$password );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接