PHP 8.5.0 Released!

Cookies

PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users. You can set cookies using the setcookie() or setrawcookie() function. Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser. This is the same limitation that header() has. You can use the output buffering functions to delay the script output until you have decided whether or not to set any cookies or send any headers.

Any cookies sent to server from the client will automatically be included into a $_COOKIE auto-global array if variables_order contains "C". If you wish to assign multiple values to a single cookie, just add [] to the cookie name.

For more details, including notes on browser bugs, see the setcookie() and setrawcookie() function.

add a note

User Contributed Notes 2 notes

up
0
twitchpaulo44 at gmail dot com
1 day ago
<?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";
}
?>

<pre>
<?php
$keys = array_keys($_POST);  
foreach ($keys as $key) {
    echo $key . ' : ' . $_POST[$key];
    echo '<br>';
    echo '<br>';
}
?>
</pre>
up
-1
twitchpaulo44 at gmail dot com
1 day ago
<?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";
}
?>

<pre>
<?php
$keys = array_keys($_POST);  
foreach ($keys as $key) {
    echo $key . ' : ' . $_POST[$key];
    echo '<br>';
    echo '<br>';
}
?>
</pre>
To Top