2016-02-15 48 views
-1

我有這個代碼來檢查登錄。它一直工作正常,但突然停止工作。

public function checkLogin($_POST) { 
     // fetching user by email 
     $stmt = $this->conn->prepare("SELECT password_hash FROM users WHERE email = ?"); 

     $stmt->bind_param("s", $email); 

     $stmt->execute(); 

     $stmt->bind_result($password_hash); 

     $stmt->store_result(); 

     if ($stmt->num_rows > 0) { 
      // Found user with the email 
      // Now verify the password 

      $stmt->fetch(); 

      $stmt->close(); 

      if (PassHash::check_password($password_hash, $password)) { 
       // User password is correct 
       return TRUE; 
      } else { 
       // user password is incorrect 
       return FALSE; 
      } 
     } else { 
      $stmt->close(); 

      // user not existed with the email 
      return FALSE; 
     } 
    } 

檢查我的Apache的錯誤日誌後,我看到這個錯誤:

PHP Fatal error: Cannot re-assign auto-global variable _POST 

任何解決此問題?

+2

你有沒有突然* *更新到新版本的PHP? :) –

+0

正如你不'''電子郵件'或'$密碼'''$ _POST'我沒有看到這個功能可以如何工作 – RiggsFolly

回答

3

自PHP 5.4,您不能使用superglobal作爲參數傳遞給函數

$_POST是全局訪問。所以你不必傳遞給你的函數。

這是你的函數應該怎麼看起來像

public function checkLogin() { 
$email = $_POST['email']; 
$password = $_POST['password']; 

     // fetching user by email 
     $stmt = $this->conn->prepare("SELECT password_hash FROM users WHERE email = ?"); 

     $stmt->bind_param("s", $email); 

     $stmt->execute(); 

     $stmt->bind_result($password_hash); 

     $stmt->store_result(); 

     if ($stmt->num_rows > 0) { 
      // Found user with the email 
      // Now verify the password 

      $stmt->fetch(); 

      $stmt->close(); 

      if (PassHash::check_password($password_hash, $password)) { 
       // User password is correct 
       return TRUE; 
      } else { 
       // user password is incorrect 
       return FALSE; 
      } 
     } else { 
      $stmt->close(); 

      // user not existed with the email 
      return FALSE; 
     } 
    } 
+0

你可能希望'$密碼'出$ _POST'他以及,在你收到他的評論之前說「沒有不行」 – RiggsFolly

+0

謝謝你的建議。完成:) – Drudge

1

那是因爲$_POST是超全球性的。 From Superglobals on PHP.net

Since PHP 5.4, you cannot use a superglobal as the parameter to a function. This causes a fatal error:

function foo($_GET) { 
    // whatever 
} 

It's called "shadowing" a superglobal, and I don't know why people ever did it, but I've seen it out there. The easy fix is just to rename the variable $get in the function, assuming that name is unique.


您可以從函數內部訪問$_POST

function foo(){ 
    print_r($_POST); 
} 

或者您也可以通過$_POST的功能,像這樣:

foo($_POST); 

function foo($post){ 
    print_r($post); 
} 
0

$_POST是超全局變量。你爲什麼通過參數傳遞它。您可以直接使用它的功能是這樣的:

public function checkLogin() { 
    print_r($_POST); 
    //your rest of code 
}