public function generateCookie() { //generate unique hash 35 characters long $cookie = substr(hash(sha256, time()), 35); //make sure the cookie doesn't already exist - just incase $this->database->processQuery("SELECT `cookie` FROM `users` WHERE `cookie` = ? LIMIT 1", array($cookie), false); if ($this->database->getRowCount() > 0) { generateCookie(); } else { return $cookie; } }
<?php include_once 'modele/authentification.php'; if (estAuthentifier()) { retourEnTerresConnues(); } if (isset($_POST['pseudoJoueur']) && isset($_POST['mdpJoueur'])) { $_SESSION['fpseudoJoueur'] = ""; $user = $_POST['pseudoJoueur']; $mdp = $_POST['mdpJoueur']; if (connexion($user, $mdp)) { //Connexion réussie $cook = generateCookie($user, $mdp); $_SESSION['connexion'] = $cook; if (isset($_POST['resterCo'])) { setcookie('connexion', $cook, time() + 3600); } retourEnTerresConnues(); } else { //Connexion échouée $_SESSION['fpseudoJoueur'] = $user; } } $warn = "Identifiants invalides !"; include_once './vue/login.php';
include_once "../inc/db_handler.php"; include_once "../inc/parameter_handler.php"; include_once "../inc/login_handler.php"; /* Detect request type */ if ($_SERVER['REQUEST_METHOD'] == "POST") { $_POST = json_decode(file_get_contents('php://input'), true); /* Fetch login parameters */ $expected = array("username", "password"); $params = fetchPostParams($expected); /* Check all parameters were included and set */ foreach ($expected as $expect) { if (!isset($params[$expect])) { die("Error: {$expect} parameter was not set."); } } $username = $params["username"]; $password = $params["password"]; /* Attempt to login */ $cookie = generateCookie($username, $password); if (!$cookie) { header('HTTP/1.1 401 Unauthorized'); echo "false"; return; } //Secret cookie setcookie("login", $cookie["secret"], $cookie["expire"], $cookie["path"], $cookie["domain"], false, true); //Userinfo cookie setcookie("userInfo", $cookie["userInfo"], $cookie["expire"], $cookie["path"], $cookie["domain"], false, false); } else { die("Unsupported request method"); }