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.



Create a Cookie in PHP

Through setcookie() function, we can create a cookie.

Syntax:

setcookie(name, value, expire, path, domain, secure, http only);

There are mostly all parameters are optional except the parameter of name.
There are given below, all the parameters explanation of the phpsetcookie() function:

ParameterExplanation
nameThe name of the cookie.
valueThe value of the cookie. The text file Do not store sensitive information since cookies value is stored on the user’s computer.
expiresThe expiry date in UNIX timestamp format. After finishing this time cookie will become inaccessible. The default value of cookie is 0 in .
pathSpecify the path on the server for which the cookie will be available. If set to /, the cookie will be available within the entire domain.
domainSpecify the domain for which the cookie value is available to e.g www.example.com.
secureThis field, if present, indicates that the cookie should be sent only if a secure HTTPS connection exists.

Pin It on Pinterest

Shares
Share This