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:
- Name: the name of the cookie.
- Value: the value of the cookie.
- Expiration: the expiration time of the cookie, which can be a timestamp or a number of seconds relative to the current time.
- 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.
- Domain: domain where the cookie is available. Default is the current domain.
- secure: Is the cookie only transmitted through a secure HTTPS connection?
- HTTPonly: Does this cookie restrict access to only HTTP protocol and prevent access via client-side scripts like JavaScript?