Использовать подготовленные операторы
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'register';
try {
$db_conn = new mysqli("$host", "$user", "$pass", "$db");
$db_conn->set_charset("utf8");
} catch (Exception $e) {
echo "Connection failed: " . $e->getMessage();
}
if(isset($_POST['register'])) {
session_start();
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
$password = password_hash($password, PASSWORD_BCRYPT);
$query = $db_conn->prepare("INSERT INTO users(username,email,password)VALUES(?,?,?)");
$query->bind_param('sss', $username, $email, $password);
$query->execute();
$counts = $query->num_rows;
$query->close();
if ($counts > 0) {
$_SESSION['message'] = " You are now logged in";
$_SESSION['username'] = $username;
header("location: Login.php");
} else {
echo 'registration failed... Something went wrong';
}
}
?>
Конец, когда пользователь пытается войти, сделать это
$query = $db_conn->prepare("select password from users where username = ? limit 1");
$query->bind_param('s', $username);
$query->bind_result($dbpassword);
$query->execute();
$query->store_result();
$query->fetch();
$query->close();
if (password_verify($password, $dbpassword)) {
echo "login success";
} else {
echo "wrong password";
}