You are currently viewing What are cookies in PHP | Explained
  • Post category:PHP

What are cookies in PHP | Explained

What are cookies in PHP

When we enter a website using cookies, we may be asked to fill out of form providing such information as our name and interests. This information is packaged into a cookie and sent to our Web browser which stores it for later use. Let’s see what are cookies in PHP.

The next time we go to the same website, our browser will send the cookie to the web server. The server can use this information to present us with custom web pages.

So, for example, instead of seeing just a generic welcome page we might see a welcome page with our name on it. PHP cookies are created using setcookie() function. All cookie data is stored in the PHP %_COOKIE global variable and accessible to subsequent pages.


setcookie(name, value, expiration, path, domain, security)

It defines a cookie to be sent along with the rest of the HTTP headers. First three arguments are mandatory while the other three are optional. Like other headers, cookies must be sent before any output from our script( this is a protocol restriction). This requires that we place calls to this function prior to any output, including tags as well as any whitespace.

Parameters Description
name The name of the cookie. The identifier is kept in the global $_COOKIE and is accessible in subsequent scripts.
value The value of the cookie. The value associated with th cookie identifier. The value is stored on the user’s computer. For this reason, the value should not contain sensitive information
expiration The time at which the cookie value expires or is no longer accessible. The expiration time can be set using the time() function. Cookies without an expiration value will expire when the browser window is closed.
path It indicates the paths on the server for which the cookie is valid or available. A forward slash “/” indicates the cookie is available to all folders.
domain The domain that the cookie is available. If no domain is specified, the default value is the value of the host on which the cookie is created. Domain values must contain at least two periods “.” in the string to be valid.
securiy Indicates whether the cookie will be transmitted via HTTPS. A value of 1 the cookie is transmitted over a secure connection and 0 denotes a standard HTTP transmission.
READ ALSO  What are Conditional Operator in PHP | Explained


Leave a Reply