Cookies in php is a small file with a maximum size that the web server stores on the client computer. they are typically used to keep track of information such as a username that the site can retrieve to personalize the page when the user visits the website next time. cookie can only be read from the domain that it has been issued from. cookies are usually set in an http header but JavaScript can also set a cookie directly on a browser.
How to set a cookie-
setcookie() function is used. The setcookie() function needs to be called prior to any output generated by the script otherwise the cookie will not be set. This function required six functions.
- Name= It is used to set the name of the cookie
- Value= It is used to set the value of the cookie.
- Expire= It is used to set the expiry timestamp of the cookie after which the cookie can’t be accessed.
- Path= It is used to specify the path on the server for which the cookie will be available.
- Domain= It is used to specify the domain for which the cookie is available.
- Security= It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists.
Syntax= setcookie(name, value, expire, path, domain, security);
one important point that we need to always remember is
- Only the name argument in the setcookie() function is mandatory. To skip an argument, the argument can be replaced by an empty string(“”).
- To check whether a cookie is set or not, the PHP isset() function is used
- If the expiration time of the cookie is set to 0, the cookie will expire at the end of the session
- The same path, domain, and other arguments should be passed that were used to create the cookie in order to ensure that the correct cookie is delete.