2017-02-18 66 views
-1

我正在練習編碼,並正在使用我從YouTube上觀看的視頻製作登錄系統。當我嘗試註冊一個帳戶時,一切正常,但唯一的問題是它給了我一個小錯誤,說明這一點:嚴格標準:只有變量應該通過/home/login-name/public_html/login/register.php中的引用傳遞線9錯誤:嚴格標準:只有變量應該通過引用傳遞?

Register.php

<?php 
require 'database.php'; 

if(!empty($_POST['username']) && !empty($_POST['password'])): 
    $sql = "INSERT INTO users (username, password) VALUES (:username, :password)"; 
    $stmt = $conn->prepare($sql); 

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

    if($stmt->execute()): 
     die('Success'); 
    else: 
     die('Fail'); 
    endif; 
endif; 
?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <title>Register</title> 
     <meta charset=utf-8> 
     <link href="../css/register.css" rel="stylesheet"> 
     <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"> 
    </head> 
    <body> 
     <div class="header"> 
      <span class="header-logo"><a href="home">Abyssal Test</a></span> 
     </div> 
     <form action="register" method="POST"> 
      <input type="text" placeholder="Username" name="username"> 
      <input type="password" placeholder="Password" name="password"> 
      <input type="password" placeholder="Confirm Password" name="confirm_password"> 
      <input type="submit" name="register" value="Register"> 
      <span class="register-text">Already have an account? <a href="login">Login Here</a></span> 
     </form> 
    </body> 
</html> 
+0

就像錯誤狀態一樣。你需要重寫'$ stmt-> bindParam(':password',password_hash($ _ POST ['password'],PASSWORD_BCRYPT));' –

回答

2

改變這一行

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

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

因爲它在函數「bindParam」的documentation中聲明瞭函數的第二個參數是一個變量引用,所以您嘗試傳遞函數結果。因此,您可以將函數的結果存儲在變量中,然後將其傳遞給函數

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

注意:HASHING不是ENCRYPTION – RiggsFolly

+0

你是對的,我的錯誤發生了變化變量名稱。 – knetsi

相關問題