2016-06-22 240 views
0

我爲使用網上找到的資源構建的網站編寫了一個登錄腳本。當我在本地服務器上運行我的代碼時,它運行良好,但現在我實際上在真正的服務器上運行它,它不起作用。我想我已經縮小了我的錯誤,但是因爲是PHP的新手而沒有使用過MySql的經驗,所以我無法真正解決我的問題。這是登錄腳本文件:數據庫登錄腳本問題

//login file 
<?php 
class Login{ 
    private $db_connection = null; 

    public function __construct(){ 
     session_start(); 
     $this->dologinWithPostData(); 
    } 

    private function dologinWithPostData(){ 

      $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 

      if (!$this->db_connection()->connect_errno) { 

       // escape the POST stuff 
       $email = $_POST['email']; 
       // database query, getting all the info of the selected user (allows login via email address in the 
       // username field) 
       $sql = "SELECT email, password 
         FROM users 
         WHERE email = '" . $email ."'"; 

       $result_of_login_check = $this->db_connection->query($sql);//This is 0 

       // if this user exists 
       if ($result_of_login_check->num_rows == 1) { 
        // get result row (as an object) 
        $result_row = $result_of_login_check->fetch_object(); 
        // using PHP 5.5's password_verify() function to check if the provided password fits 
        // the hash of that user's password 
        if ($_POST['password'] == $result_row->password) { 
         // write user data into PHP SESSION (a file on your server) 
         $_SESSION['email'] = $result_row->email; 
         $_SESSION['user_login_status'] = 1; 
        } else { 
         $this->errors[] = "Wrong password. Try again."; 
         $_SESSION['user_login_status'] = 0; 
        } 
       } else { 
        $this->errors[] = "This user does not exist."; 
       } 
      } else { 
       $this->errors[] = "Database connection problem."; 
      } 
     } 

    print_r($this->errors); 
    } 

    public function isUserLoggedIn() 
    { 
     if (isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] == 1) { 
      return true; 
     } 
     // default return 
     return false; 
    } 
} 

?> 

我在另一個文件本質上是運行如下:

//Run file 
require_once("dbconfig.php"); 
    include_once("login.php"); 
    $login = new Login(); 
    if($login->isUserLoggedIn() == true){ 
    //go to another page } 

用於訪問數據庫的變量實例在dbconfig.php和是正確的。有了這段代碼,我得到一個錯誤,說該頁面無法正常工作,無法處理請求。當我註釋掉該行

if (!$this->db_connection()->connect_errno) { 

和其後的else語句,輸出是「該用戶不存在」。所以我認爲這個錯誤與$this->db_connection()->connect_errno)有關。如果你能找到我出錯的地方,或者對如何改寫腳本以使其更好的建議有任何建議,我們將不勝感激。

+0

你很容易[sql注入攻擊](http://bobby-tables.com) –

+0

http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection -in-php – Scott

+2

不,不,不!您不會將用戶密碼存儲在數據庫中。你沒有。你存儲的是用戶密碼hash_,由_good_ hashing算法創建。然後,在登錄時,您散列提供的密碼和_compare兩個哈希值。這樣,如果您的服務受到威脅,您不會冒用戶密碼的風險。 – arkascha

回答

1

這是一個數據庫建立錯誤的現場遠程服務器數據庫配置different.Please驗證你dbconfig.php文件確保 數據庫名,主機,端口,用戶名,密碼,非常符合你的活數據庫中定義

1

這是錯誤:

 if (!$this->db_connection()->connect_errno) { 

db_connection只是一個包含您的數據庫連接對象的變量。它是不是的一種方法。

你可能想

 if (!$this->db_connection->connect_errno) { 
           ^--note lack of() 

代替。

0

我覺得這個問題與follwoing檢查。你的結果得到超過1條記錄。 //如果此用戶存在

if ($result_of_login_check->num_rows == 1) { 
...... 
}else{ 
    $this->errors[] = "This user does not exist."; 
} 

確保您的電子郵件地址是在數據表中是唯一的,如果是不唯一那麼你上面的語句將失敗,並顯示文本「此用戶不存在。」從其他部分