PHP Cookies

PHP Cookies

A cookie is a text file used to identify a user.  It is a file the server embeds on the client computer. You can easily create, retrieve cookie values, and also delete it.

PHP Cookies Syntax

setcookie() function is used to create cookies in PHP. Here’s the syntax,


  • name = Name of the cookie. Environment variable HTTP_COOKIE_VARS stores cookies.
  • value =The content of the cookie.
  • expire = Sets the expiry date of the cookie, after which it will become inaccessible.
  • path = Path is the directories for which the cookie will be valid. For allowing the validity of
    cookies for all directories, set a single forward slash character.
  • domain = domain name
  • secure = Set  to 1 for sending cookie by secure transmission i.e. HTTPS, Set to 0 for sending cookie by HTTP

Set and access Cookie

Here, you can see first we will set cookie for Student Marks. We have also set the cookie value, and expiry date of the cookie.

To check if a cookie is set or not, we’re using, isset() function,

Here’s the output,

PHP Cookies

Delete PHP Cookie

Using expiration date in the past, deletes a cookie. Let’s see how we can use it easily,

Deleting PHP Cookie

PHP Functions
PHP Sessions
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create free tutorials for all.

No Comments

Post A Comment