PHP set cookie - free php hosting -

PHP Set Cookie

PHP Summer Training/Internship with 2 Live Projects in Noida Fee is only 5500/-Click here to Book Your Seat


PHP Set Cookie

A cookie is a text file saved to a user’s system by a Web site. This file contains information that the site can retrieve on the user’s next visit, thereby allowing the site to “recognize” the user and provide an enhanced set of features customized to that specific user.

Cookie have some important security features :

  • A cookie can only be read by the Web site or domain that created it.
  • A single domain cannot set more than 20 cookies.
  • A single cookie cannot exceed 4 kilobytes in size.
  • The maximum number of cookies that may be set on a user’s system is 300.

Set Cookie in PHP

The setcookie( ) function is used to set a cookie.

Note : The setcookie( ) function must use BEFORE the <html> tag.

In the given example, create a cookie named “cookie_user” and assign the value “abhi” to it. We also specify that the cookie should expire after one hour.

Explain : In the example above cookie_name variale creates and assign value inside variable is “abhi” which work for 1 hour.

PHP get Cookie

The $_COOKIE[ ] super global variable is used to retrieve a cookie value.

In the example below, we retrieve the value of the cookie named “cookie_name” and display it on a page:

Welcome abhi

Note : It works for 1 hour after 1 hour cookie deleted automatically and if you try to access after destroyed cookie it gives an error message.

Set Multiple Cookie and get all Cookie

Enter your name
Enter your age
Enter your profile

Retrieve all cookies

Array ( [user] => abhi [age] => 25 [profile] => developer )

PHP delete Cookie

When deleting a cookie you should assure that the expiration date is in the past.

Latest Trending Technologies