How to make cookies php

Learn how to make PHP cookies with a simple example: create, read, edit, and delete cookies in PHP.

Making cookies in PHP

Cookies are a great way to store data on a user's computer, and fortunately, they're easy to create and access in PHP. Cookies can be used to store a wide variety of data, including user settings, login details, and preferences.

To create a cookie in PHP, the setcookie() function is used. This function takes three parameters:

  • name - The name of the cookie
  • value - The value of the cookie
  • expire - The expiration date of the cookie (in Unix timestamp format)

Here's an example of how to create a cookie with the setcookie() function:

setcookie('my_cookie', 'my_value', time() + (86400 * 30)); // 86400 = 1 day

This sets a cookie called "my_cookie" with a value of "my_value". The cookie will expire in 30 days (86400 seconds is equal to 1 day).

Once the cookie has been set, it can be accessed using the $_COOKIE array. Here's an example of how to access the value of the cookie:

$my_value = $_COOKIE['my_cookie'];

This will set the $my_value variable to the value of the cookie (in this case, "my_value").

Cookies are a powerful way to store data on a user's computer, and they're easy to create and access in PHP. With the setcookie() and $_COOKIE functions, you can easily create and access cookies in your PHP scripts.

Answers (0)