In this tutorial, we will see how to store a minimum amount of data within the user’s browser itself using the PHP cookies.
What is a Cookie?
A cookie is a small text file that lets you store a small amount of information (nearly 4KB) on the user’s computer.
Each time with the same computer the browser requests a page to the server and all the information in the cookie is automatically sent to the server within the request.
And mostly cookie is frequently used to recognize the user. With the help of PHP, you can both create and retrieve cookie values.
How to set a Cookie in PHP
Through setcookie()
function, we can create or set a cookie.
Syntax:
setcookie(name, value, expire, path, domain, secure, http only);
There are mostly all parameters are optional except the parameter of the name.
There are given below, all the parameters explanation of the phpsetcookie()
function:
Parameter | Explanation |
---|---|
name |
The name of the cookie. |
value |
The value of the cookie. The text file Do not store sensitive information since cookies value is stored on the user’s computer. |
expires |
The expiry date in UNIX timestamp format. After finishing this time cookie will become inaccessible. The default value of cookie is 0 in . |
path |
Specify the path on the server for which the cookie will be available. If set to / , the cookie will be available within the entire domain. |
domain |
Specify the domain for which the cookie value is available to e.g www.example.com. |
secure |
This field, if present, indicates that the cookie should be sent only if a secure HTTPS connection exists. |