Skip to content

Instantly share code, notes, and snippets.

@CuzImBisonratte
Created January 7, 2022 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CuzImBisonratte/bdcaff57afdede91c5bcb1859bec3c06 to your computer and use it in GitHub Desktop.
Save CuzImBisonratte/bdcaff57afdede91c5bcb1859bec3c06 to your computer and use it in GitHub Desktop.
Login - Password checker
// Connect to the database
$con = new mysqli($db_host, $db_user, $db_pass, $dbname);
// Check connection
if ($con->connect_error) {
exit("Connection failed: " . $con->connect_error);
}
// Get the form input
$username = $_POST['username'];
$password = $_POST['password'];
// Check if the username and password are correct
if ($stmt = $con->prepare('SELECT password FROM accounts WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc)
$stmt->bind_param('s', $username);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
// Check if Account exist
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
if (password_verify($_POST['password'], $password)) {
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
header('Location: ../app/');
exit;
} else {
exit("Wrong password");
}
} else {
exit("Wrong username");
}
} else {
exit("Error: " . $con->error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment