Php $\u POST突然抛出错误
我有这个检查登录的代码。它一直工作正常,但只是突然停止工作Php $\u POST突然抛出错误,php,mysql,apache,Php,Mysql,Apache,我有这个检查登录的代码。它一直工作正常,但只是突然停止工作 public function checkLogin($_POST) { // fetching user by email $stmt = $this->conn->prepare("SELECT password_hash FROM users WHERE email = ?"); $stmt->bind_param("s", $email); $
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
有没有解决这个问题的办法?这是因为
$\u POST
是一个超全局的:
由于PHP5.4,您不能使用超全局函数作为函数的参数。这将导致一个致命错误:
function foo($_GET) {
// whatever
}
它被称为“阴影”,是一种超全球现象,我不知道人们为什么会这样做,但我在那里看到过。简单的解决方法是在函数中重命名变量$get,假设该名称是唯一的
您可以从函数中访问
$\u POST
:
function foo(){
print_r($_POST);
}
或者您可以将$\u POST
传递给如下函数:
foo($_POST);
function foo($post){
print_r($post);
}
$\u POST
是超全局变量。为什么要在参数中传递它。您可以在如下功能中直接使用它:
public function checkLogin() {
print_r($_POST);
//your rest of code
}
由于PHP5.4,您不能使用a作为函数的参数
$\u 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;
}
}
您是否突然更新到较新的PHP版本?:)由于您没有从
$\u POST
中挖掘$email
或$password
,我不知道这个功能是如何工作的。您可能也想从$\u POST
中获得$password
,然后再从他那里得到一条评论,说“不仍然不起作用”。谢谢您的建议。完成:)