работа с cookies

PHP – working with cookies

Hi all! Today – a small post on the topic: PHP – working with cookies.
I’m sure you know what these same cookies are. Just in case, this is a small code that is used to:

  • user authentication
  • storing personal preferences and user settings
  • tracking user access session state
  • user statistics information

Cookies are generated on the server and stored on the user’s computer.
You can create these same cookies (cookies) in PHP as follows:

setcookie(cookie_name, value_cookie, time() + 3600);

where:
– cookie_name – cookie name
– value_cookie – cookie value
– time() – cookie lifetime in seconds, after which it will be automatically deleted

BUT! The fact that a cookie has an expiration date does not mean that it cannot be deleted. It is removed simply:

setcookie(cookie_name, value_cookie, time() - 3600);

those. the deletion code is completely similar to the code for creating cookies, with one difference – we put not a plus, but a minus in the cookie lifetime. As you can see, everything is simple!
Let’s take a look at the example code in practice. I created two pages here: https://lavrynenko.com/php_cookie/index_cookie.php – (responsible for the value of the cookie with the name “cookie_name” (yes, I’m original 🙂) and displaying this cookie itself) and https://lavrynenko. com/php_cookie/add_cookie.php – which, in fact, creates this very cookie.

The source code of the pages is below:
index_cookie.php

<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Куки-текст</title>
</head>
<body>
<form action="add_cookie.php" method="post">
<input type="text" name="value_cookie" placeholder="Введите значение куки" /><br />
<input type="submit" name="add_cookie" value="Добавить куку">
</form>
<?php
if (isset($_COOKIE['cookie_name'])) {
echo $_COOKIE['cookie_name'];
} else {
echo "Кука не установлена";
}
?>
</body>
</html>

Let’s analyze the code in more detail:

<form action="add_cookie.php" method="post">

we create a form that will be processed by the code located in a file called add_cookie.php (I talked about this in more detail here.)

 <input type="text" name="value_cookie" placeholder="Введите значение куки" /><br />
<input type="submit" name="add_cookie" value="Добавить куку">

We have a field in which we enter the desired cookie value, and a button, by clicking on which we get into the add_cookie.php file – which will do all the magic.

<?php
if (isset($_COOKIE['cookie_name'])) {
echo $_COOKIE['cookie_name'];
} else {
echo "Кука не установлена";
}
?>

This code is responsible for displaying the cookie saved on the computer – in case we do not have cookies with the desired name, we display a message that the cookie is not set. Otherwise, output the value of the cookie.

With the first file sorted out. Now we look at the code of the file that is responsible for creating the cookie:

<?php
$value_cookie = $_POST['value_cookie'];
setcookie("cookie_name", $value_cookie, time() + 3600, '/');
header("Location: index_cookie.php");
exit();
?>

everything is simple here:
in the first line we get the cookie value that we set on the first page
the second line is responsible for creating the cookie: cookie name: “cookie_name”, its value is the variable we created in the first line, the cookie lifetime is 3600 seconds.
the third line is a redirect to our first page (it is important for us to see the result of processing the file).
I hope it became clearer to you how the work with cookies in PHP is organized. As always, if you have any questions, write to the mail or Telegram. Thanks!