How to use setcookie() function in PHP ? Last Updated : 16 Mar, 2022 Comments Improve Suggest changes Like Article Like Report A cookie is often a small file that is embedded by the server from which the user has visited or is getting a response. Each time the computer requests a page within a browser, it will send a cookie. Using PHP we can do both create and retrieve cookie values. A variable is automatically created with the same name that is of the cookie. For example, if a cookie was sent with the name "client", a variable of name "client" is automatically created containing the cookie i.e $client. Cookies are sent along with the HTTP headers. Like other headers, cookies should be sent before any output from your script. Create Cookie: The setcookie() function is used to create a cookie. The setcookie() function defines a cookie to be sent along with other HTTP headers. The setcookie() function should be appeared before the <html> and <head> tag. Syntax: setcookie(name, value, expire, path, domain, secure, httponly); Parameters: name: It is required. It specifies the name of the cookie to be sent.value: It is optional. It specifies the value of the cookie to be sent.expire: It is optional. It specifies when the cookie will expire. It has a default value of 0, which determines that the cookie will expire on the closing session (closing the browser).path: It is optional. It specifies the server path of the cookie. Its default value is the current directory that the cookie is being set in.domain: It is optional. It specifies the domain name of the cookie. For making the cookie available on all subdomains of "example.com", set it to "example.com".secure: It is optional. It specifies whether cookies should be only transmitted over a secure HTTPS connection. The default value is "false" (cookie will set on any connection).httponly: It is optional. If set to TRUE, the cookie will be accessible only through the HTTP protocol. Default is FALSE. Returns: It returns true on success.It returns false on failure. Example 1: PHP <?php $value = 'Arecookiesset'; setcookie("TestCookie", $value); setcookie("check","are cookies set") ?> <?php // Print an individual cookie // echo $_COOKIE["TestCookie"] ."\n"; // echo $_COOKIE["check"] ."\n"; // Another way to debug/test is to view all cookies print_r($_COOKIE); ?> Output: Array ( [TestCookie] => Arecookiesset [check] => are cookies set ) Example 2: In this example, we are deleting the cookie name "check". PHP <?php $value = 'Arecookiesset'; setcookie("TestCookie", $value); setcookie("check","are cookies set"); //deleting the cookie of name check setcookie("check","",time()-3600); ?> <?php // Print an individual cookie // echo $_COOKIE["TestCookie"] ."\n"; // echo $_COOKIE["check"] ."\n"; // Another way to debug/test is to view all cookies print_r($_COOKIE); ?> Output: Array ( [TestCookie] => Arecookiesset ) Comment More infoAdvertise with us Next Article How to use setcookie() function in PHP ? M mahalenachiket77 Follow Improve Article Tags : PHP Similar Reads PHP | Imagick setOption() Function The Imagick::setOption() function is an inbuilt function in PHP which is used to set an option. Syntax: bool Imagick::setOption( string $key, string $value ) Parameters: This function accepts two parameters as mentioned above and described below: $key: It specifies the key for the option. $value: It 1 min read PHP | settype() Function The settype() function is a built-in function in PHP. The settype() function is used to the set the type of a variable. It is used to set type or modify type of an existing variable. Syntax: boolean settype($variable_name, $type) Parameters: The settype() function accepts two parameters as shown in 2 min read PHP | Gmagick setimageunits() Function The Gmagick::setimageunits() function is an inbuilt function in PHP which is used to set the units of resolution of a particular image. This function has no visual impact on the image but just changes the units of resolution which can be one of Undefinedresolution, PixelsPerInchResolution, or Pixels 1 min read PHP | ftp_set_option() Function The ftp_set_option() function is an inbuilt function in PHP which is used to set runtime option for existing FTP Connection. Syntax: ftp_set_option( $ftp_connection, $option, $value ) Parameter: This function accepts three parameters as mentioned above and described below:  $ftp_connection: It is 2 min read How to Remove a Cookie in PHP? Cookies are small text files stored in a user's browser. It contain data such as user preferences, session information, and other relevant details. PHP cookies are used to store information that can persist across different web pages or sessions. At times, you may need to delete or remove a cookie, 4 min read PHP openssl_spki_verify() Function The openssl_spki_verify() function is a built-in function in PHP and is used to validate the supplied signed public key and challenge. This should be the public key corresponding to the private key used for the signature. It verifies a signed public key and challenge. Syntax: string openssl_spki_ver 2 min read How to add API function to a simple PHP Page ? Application Programming Interface is a system that contains a set of rules or protocols or tools which help in providing interaction between two applications or software by standard data access. It is very much similar to a web service used in developing web pages or mobile apps. One application can 4 min read PHP | setlocale() Function The setlocale() function is an inbuilt function in PHP which is used to set locale information. Locale setting means assigning your system a geographical location and then perform certain functions based on the locale of the place. Usually, programs dealing with the date and time of other places dea 3 min read How to Use API Keys authentication in Postman Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how to use API Keys authentication in Postman. The API key is a unique identifier that authenticates requests and if several users are there, their username 2 min read PHP | Gmagick setimageprofile() Function The Gmagick::setimageprofile() function is an inbuilt function in PHP which is used to add a named profile to the Gmagick object. Syntax: Gmagick Gmagick::setimageprofile( string $name, string $profile ) Parameters: This function accepts two parameters as mentioned above and described below: $name: 1 min read Like