PHP Cookies

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

Cookies Syntax

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

Here,

  • 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 cookies by secure transmission i.e. HTTPS, Set to 0 for sending cookies by HTTP

Set and access Cookies

Here, first we will set cookies 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 the isset() function. Let us see an example:

Here’s the output,

PHP Cookies

Delete Cookies

Using an expiration date in the past deletes a cookie. Let’s see an example and check how we can use it easily:

Deleting PHP Cookie

PHP Functions
PHP Sessions
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading