What is the method for setting a cookie in PHP?

In PHP, the method for setting a cookie is to use the setcookie() function. The syntax of this function is as follows:

Create a cookie by using the setcookie function with parameters for name, value, expiration date, path, domain, security, and HTTP only.

Description of parameters:

  1. Name: the name of the cookie.
  2. Value: the value of the cookie.
  3. Expiration: the expiration time of the cookie, which can be a timestamp or a number of seconds relative to the current time.
  4. The path determines where the cookie is accessible. If set to “/”, the cookie can be accessed on all pages within the domain. If set to “/test/”, only pages within the /test/ directory and its subdirectories can access the cookie.
  5. Domain: domain where the cookie is available. Default is the current domain.
  6. secure: Is the cookie only transmitted through a secure HTTPS connection?
  7. HTTPonly: Does this cookie restrict access to only HTTP protocol and prevent access via client-side scripts like JavaScript?
bannerAds