note 130551 added to reserved.variables.cookies

From: Date: Fri, 21 Nov 2025 08:17:31 +0000
Subject: note 130551 added to reserved.variables.cookies
Groups: php.notes 
Request: Send a blank email to php-notes+get-222427@lists.php.net to get a copy of this message
<?php $pseudo = ''; $password = ''; if (!empty($_COOKIE['pseudo'])) { $pseudo = $_COOKIE['pseudo']; } if (!empty($_COOKIE['password'])) { $password = $_COOKIE['password']; } ?> <!doctype html> <html lang="fr"> <head> <meta charset="utf-8"> <title>Formulaire persistant</title> </head> <body> <form action="authentificate.php" method="post"> <div> <label for="pseudo">Nom</label><br> <input type="text" id="pseudo" name="pseudo" value="<?php echo htmlentities($pseudo); ?>" required> </div> <div> <label for="password">Mot de passe</label><br> <input type="password" id="password" name="password" value="<?php echo htmlentities($password); ?>" required> <?php if (!empty($_GET['retry'])): ?> <p style="color: red;">Nom ou mot de passe incorrect</p> <?php endif; ?> </div> <div> <button type="submit">Se connecter</button> </div> </form> </body> </html> <?php $pseudo = $_POST['pseudo'] ?? ''; $password = $_POST['password'] ?? ''; $users = array( "jojo" => array("password" => "pass1", "status" => "administrator"), "raoul" => array("password" => "pass2", "status" => "visitor"), "roméo" => array("password" => "pass3", "status" => "customer"), ); function authenticate(string $pseudo, string $password, array $users): bool { if ($pseudo === '' || $password === '') { return false; } if (!isset($users[$pseudo])) { return false; } return $users[$pseudo]['password'] === $password; } if (!authenticate($pseudo, $password, $users)) { header('Location: persistent_form.php?retry=1'); exit(); } setcookie(name: "pseudo", value: $pseudo); setcookie(name: "password", value: $password); session_start(); $_SESSION['pseudo'] = $pseudo; $_SESSION['status'] = $users[$pseudo]['status']; header('Location: site.php'); ?> <?php session_start(); $pseudo = $_SESSION['pseudo'] ?? 'Guest'; $status = $_SESSION['status'] ?? 'unknown'; if ($status === 'customer') { echo htmlentities($pseudo); echo "<br>"; echo "<br>"; echo "consulter, acheter"; } else if ($status === 'administrator') { echo htmlentities($pseudo); echo "<br>"; echo "<br>"; echo "consulter, acheter, administrer"; } else if ($status === 'visitor') { echo htmlentities($pseudo); echo "<br>"; echo "<br>"; echo "consulter"; } else { echo "acces refuse"; } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 2001:660:7220:385:193:52:103:33) ---- Manual Page -- https://php.net/manual/en/reserved.variables.cookies.php Edit -- https://main.php.net/note/edit/130551 Del: integrated -- https://main.php.net/note/delete/130551/integrated Del: useless -- https://main.php.net/note/delete/130551/useless Del: bad code -- https://main.php.net/note/delete/130551/bad+code Del: spam -- https://main.php.net/note/delete/130551/spam Del: non-english -- https://main.php.net/note/delete/130551/non-english Del: in docs -- https://main.php.net/note/delete/130551/in+docs Del: other reasons-- https://main.php.net/note/delete/130551 Reject -- https://main.php.net/note/reject/130551 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222427) next »