I'm using code that I've used on another webpage, which works fine on the other page, but not on this new one. The MySQLi table for logins is setup exactly the same too. I don't think it's the login script, because I inserted my password hash from the other site into the database to test it, and I can login with it.
This is the registration code:
include('sql.php');
$username = $_POST['username'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];
if($username == '' || $password = '') {
header('Location:/register.php');
}
if($password != $confirm) {
header('Location:/register.php');
}
$sql = "INSERT INTO login (username, password) VALUES ('" . $username . "', '" . password_hash($password, PASSWORD_DEFAULT) . "')";
if(mysqli_query($mysqli, $sql)) {
header('Location: /dashboard.php');
} else {
echo $mysqli->error;
}
mysqli_close($mysqli);
And the login:
session_start();
$error = '';
if(isset($_POST['submit'])) {
if(empty($_POST['username']) || empty($_POST['password'])) {
header("Location: /admin.php?error=invalid");
} else {
include('sql.php');
$username = mysqli_real_escape_string($mysqli, stripslashes($_POST['username']));
$password = mysqli_real_escape_string($mysqli, stripslashes($_POST['password']));
$sql = "SELECT * FROM login WHERE username='" . $username . "'";
$result = $mysqli->query($sql);
if($result->num_rows == 1) {
while($row = $result->fetch_assoc()) {
$verify = password_verify($password, $row['password']);
if($verify == false) {
header("Location: /admin.php?error=mismatch");
} else {
$_SESSION['login_user'] = $username;
$_SESSION['login_pass'] = $password;
if($_POST['stay'] == 'stay') {
setcookie('username', $username, time() + 31536000, '/');
setcookie('password', $password, time() + 31536000, '/');
}
header("location: /dashboard.php");
}
}
} else {
header("Location: /admin.php?error=mismatch");
}
mysqli_close($mysqli);
}
}