работа с 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







Let’s analyze the code in more detail:

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.)


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.


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:


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!

Support the Blog!

Running a blog takes a lot of effort, time, and passion. Your donations help improve the content, inspire new ideas, and keep the project going.
If you’ve enjoyed the blog’s materials, any support would mean the world to me. Thank you for being here! ❤️

PayPal Logo Donate via PayPal

Revolut Logo Donate via Revolut