2017-07-15 81 views
1

我很困惑, 不久前,我被告知我需要哈希我的密碼,我認爲我已經完成了,我看了一個單獨的溢出問題在這裏找到How to use password_hash我的PHP代碼似乎並不正確

但遺憾的是,我試圖將其添加到我的代碼,似乎沒有任何工作,另一件事是我被告知添加http://php.net/manual/en/mysqli.construct.php這使我更困惑。我覺得我的代碼沒有一個是正確的,我覺得自己完全不知道任何這些,我真的很抱歉。我問過我的老師誰4年前做過PHP,但可惜她也不知道。我真的想在編碼方面做得更好,但我覺得我不知道這一點。我真的盡我最大努力做到這一點,不要問溢出,因爲我覺得我做的一切都是錯的:/。

我曾嘗試在php網站上做很多研究,並且我已經到處尋找可能的答案,關於如何添加mysqli_construct。顯然,我有機會獲得我的代碼注入。我知道這可能是很簡單的修復,但我完全困惑的一切,

再次感謝如果你能幫助我。

大衛,

-Code-

的index.php

<?php 
    include("database.php"); 
    session_start(); 

    if($_SERVER["REQUEST_METHOD"] == "POST") { 

     // Create querystring 

     $sql = "SELECT id, password FROM admin WHERE username = ?"; 

     // Prepare, bind, execute 

     $stmt = mysqli_prepare($db,$sql); 
     mysqli_stmt_bind_param($stmt, 's', $_POST['username']); 
     mysqli_stmt_execute($stmt); 
     mysqli_stmt_bind_result($stmt, $user_id, $user_password); 
     if (mysqli_stmt_fetch($stmt)) { 

     // Validate password 

     if (password_verify($_POST['password'], $user_password)) { 
      session_register("username"); 
      $_SESSION['login_user'] = $username; 

      header("location: myaccount.php"); 
      exit; 
     } else { 
      $error = "Your Login Name or Password is invalid"; 
     } 
     mysqli_stmt_close($stmt); 
     } else { 
     $error = "Your Login Name or Password is invalid"; 
     } 
    } 
?> 

database.php中

<?php 

$host = 'localhost'; 
$user = '-'; 
$pass = '-'; 
$db = 'database'; 
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error); 

?> 

我的錯誤日誌

[15-Jul-2017 05:29:20 America/New_York] PHP Warning: mysqli_prepare() expects parameter 1 to be mysqli, string given in /home/beaskxxb/public_html/index.php on line 10 
[15-Jul-2017 05:29:20 America/New_York] PHP Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in /home/beaskxxb/public_html/index.php on line 11 
[15-Jul-2017 05:29:20 America/New_York] PHP Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, null given in /home/beaskxxb/public_html/index.php on line 12 
[15-Jul-2017 05:29:20 America/New_York] PHP Warning: mysqli_stmt_bind_result() expects parameter 1 to be mysqli_stmt, null given in /home/beaskxxb/public_html/index.php on line 13 
[15-Jul-2017 05:29:20 America/New_York] PHP Warning: mysqli_stmt_fetch() expects parameter 1 to be mysqli_stmt, null given in /home/beaskxxb/public_html/index.php on line 14 

有人說我需要做一個function.php?我深入瞭解了這一點,因爲顯然我沒有定義所有的東西, 我只是真的想要這個工作。因爲它似乎正在倒退, 對不起,我知道它不是那麼好。但我只是希望它的工作,

編輯: 管理的表結構:

1 username longtext latin1_swedish_ci Yes NULL Change Change Drop Drop 
2 password longtext latin1_swedish_ci No None Change Change Drop Drop 

你的大衛。

+1

你'mysqli_prepare($分貝,$ SQL);'應該是'mysqli_prepare ($ mysqli,$ sql);'當你傳入數據庫名時,而不是連接。 –

+1

如果您想使用MySQLi的過程風格,請使用'mysqli_connect(...)'而不是'new mysqli(...)'。不要混用這兩種風格。 – PeterMader

+0

好的!謝謝你們的幫助,我改變了風格。我試圖用'mysqli_prepare($ mysqli,$ sql)連接;'但是我現在似乎正在獲取這個error_log? https://pastebin.com/qQpi5Tpq – BeastFox

回答

2

首先 - 改變

$stmt = mysqli_prepare($db,$sql); 

$stmt = mysqli_prepare($mysqli,$sql); 

你正經過的數據庫名稱,而不是連接到數據庫。

如果你沒有一個名爲id在表列,那麼你可以簡單地使用

$sql = "SELECT password FROM admin WHERE username = ?"; 

正如您剛纔檢查密碼,這就是你所需要的。

要檢查您的準備工作是否正常,請更改以下幾行...

if (!$stmt = mysqli_prepare($mysqli,$sql)) { 
    echo "Failed to prepare:".mysqli_error($mysqli); 
    return; 
} 

編輯: 從選擇去除ID後,您需要更改綁定到...

mysqli_stmt_bind_result($stmt, $user_password); 
+0

我想我們可能會取得一些進展,你是對的。我在表中沒有一個名爲id的列,所以我改變了這一點,但我似乎現在正在得到這個錯誤? https://pastebin.com/s6ptug0z – BeastFox

+0

添加錯誤檢查(只是添加到答案),看看是否給你任何提示問題。 –

+0

這是我後來想改變的代碼嗎? 'mysqli_stmt_close($語句); }其他{ $ error =「您的登錄名或密碼無效」; }' – BeastFox