Comment gérer les cookies en PHP ?

Asked  Jan 01, 01:06 am 31 Views Add Your Answer
Asked  Jan 01, 01:06 am
Php
0 Dislikes
0 Comments

1 Answers

Added  Jan 02, 12:46 am

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Set Cookie

<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

Get Cookie

<?php
if(!isset($_COOKIE[$cookie_name])) {
  echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
  echo "Cookie '" . $cookie_name . "' is set!<br>";
  echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

1 Likes 0 Dislikes
0 Comments
×
SignUp with Email
X