For me works something like this:
<?php session_start(); include_once dbconnect.php ; if (mysqli_connect_errno()) { // If there is an error with the connection, stop the script and display the error. header( location:/error.php ); }
if ( !isset($_POST[ emailuser ], $_POST[ userPass ]) ) {
// Could not get the data that should have been sent.
die ( Please fill both the username and password field! );
}
$emailuser = ($_POST[ emailuser ]);
$emailuser = trim($emailuser);
if ($stmt = $con->prepare( SELECT userEmail or userName, userPass FROM users WHERE userEmail = ? or userName = ? )) {
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
$stmt->bind_param( ss , $emailuser, $emailuser);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($userName, $userPass);
$stmt->fetch();
// Account exists, now we verify the password.
// Note: remember to use password_hash in your registration file to store the hashed passwords.
if (password_verify($_POST[ userPass ], $userPass)) {
// Verification success! User has loggedin!
// Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION[ loggedin ] = true;
$_SESSION[ name ] = $emailuser;
$_SESSION[ emailuser ] = $userName;
header( location: /menu.php );
} else {
echo Incorrect password! ;
}
} else {
echo Incorrect username! ;
}
$stmt->close(); } ?>