This question already has an answer here:
I have this code that checks for login. It has been working fine but just stopped working suddenly.
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;
}
}
After checking my apache error log, I am seeing this error:
PHP Fatal error: Cannot re-assign auto-global variable _POST
Any work around this?
</div>
Since PHP 5.4, you cannot use a superglobal as the parameter to a function
$_POST
is globally accessible. So you don't have to pass to your function.
This is how your function should look like
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;
}
}
That is because $_POST
is a superglobal. 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.
You can access $_POST
from within the function:
function foo(){
print_r($_POST);
}
Or you can pass $_POST
to a function like so:
foo($_POST);
function foo($post){
print_r($post);
}
$_POST
is superglobal variable. Why are you passing it in parameter. You can directly use it in function like this :
public function checkLogin() {
print_r($_POST);
//your rest of code
}