dprfe04886 2014-04-14 17:19
浏览 120
已采纳

为什么PHP的“password_hash”不返回字符串?

<?php
    $connection=new PDO("mysql:host=localhost;dbname=userdata", "secure_credentials", "battery_staple");
    $user=$_POST['username1'];
    $pass=$_POST['password1'];
    $snip=mb_substr($user, 0, 3);
    $pass=password_hash($pass, PASSWORD_BCRYPT);
    $user_query=$connection->prepare("INSERT INTO login (email, password, semod, snippet) VALUES (:email, :password, :semod, :snippet)");
    $user_query->bindParam(':email', $user);
    $user_query->bindParam(':password', $pass);
    $user_query->bindParam(':semod', "false");
    $user_query->bindParam(':snippet', $snip);
    $user_query->execute;

(Password changed)

I'm facing a small problem with the above PHP code, whenever it's executed I get this error:

Fatal error: Cannot pass parameter 2 by reference in [location] on line [##: I shortened the above code... it's the password field with issues]

Looking around, this seems to be an issue when passing an string/integer directly, without using a variable. However, password_hash() returns a string, so I am led to believe that it's not returning a string. What could be the problem of this issue?

  • 写回答

1条回答 默认 最新

  • doubutao6216 2014-04-14 17:27
    关注

    Your error is on this line:

    $user_query->bindParam(':semod', "false");
    

    You need to use bindValue.

    $user_query->bindValue(':semod', "false");
    

    bindParam passes the second argument by reference, so it has to be a variable, not a literal.

    Additionally, known values don't need to be bound. You could have just as easily added the literal 'false' string to your statement query, ie

    "INSERT INTO login (email, password, semod, snippet) VALUES (:email, :password, 'false', :snippet)"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?