Code Snippets
Login
Basic login function
Don't forget to rename the fields :D
Label
// PROCEDURAL VERSION function attemptUserLogin(PDO $db, string $name, string $pwd) : bool { $sql = 'SELECT * FROM `snippets_users` WHERE `snip_user_login` = :name'; $stmt = $db->prepare($sql); $stmt->bindParam(':name', $name); $stmt->execute(); if ($stmt->rowCount() === 0) return false; $user = $stmt->fetch(); if (!password_verify($pwd, $user['snip_user_pass'])) return false; $_SESSION = $user; unset($_SESSION["snip_user_pass"]); $_SESSION["id"] = session_id(); return true; } // OO VERSION public function attemptUserLogin(string $name, string $pwd): bool { $name = htmlspecialchars(strip_tags(trim($name))); $sql = 'SELECT * FROM `snippets_users` WHERE `snip_user_login` = :name'; $stmt = $this->db->prepare($sql); $stmt->bindParam(':name', $name); $stmt->execute(); if ($stmt->rowCount() === 0) { return false; } $row = $stmt->fetch(); $user = new UserMapping($row); if (!password_verify($pwd, $user->getSnipUserPass())) { return false; } $_SESSION["id"] = session_id(); return true; }
Copy to clipboard
Copied!
©
Leerlandais 2024