Tags
Asked 2 years ago
17 Jun 2021
Views 164
Pietro

Pietro posted

Creating cookies in php

Creating cookies in php
Mahesh Radadiya

Mahesh Radadiya
answered Apr 27 '23 00:00

In PHP, you can create cookies using the setcookie() function. Here's the basic syntax:



setcookie(name, value, expire, path, domain, secure, httponly
);
Let's break down each parameter:

name : the name of the cookie (required).
value : the value of the cookie (optional).
expire : the expiration time of the cookie, in Unix timestamp format (optional). If not specified, the cookie will expire at the end of the session (when the browser is closed).
path : the path on the server where the cookie will be available (optional). If not specified, the cookie will be available for the entire domain.
domain : the domain where the cookie will be available (optional). If not specified, the cookie will be available for the current domain only.
secure: a Boolean value indicating whether the cookie should only be transmitted over a secure HTTPS connection (optional).
httponly : a Boolean value indicating whether the cookie should be accessible only through HTTP requests, and not through client-side scripts (optional).
Here's an example of how to create a cookie in PHP:



setcookie("username", "john_doe", time() + (86400 * 30), "/");

In this example, we create a cookie called username with the value john_doe , and an expiration time of 30 days from the current time. The cookie will be available for the entire domain (since we set the path to /).

Note that cookies must be set before any output is sent to the browser, or they will not be set. Additionally, cookies can be manipulated by the user, so they should not be used to store sensitive information such as passwords or other confidential data.
Post Answer