Php tutorials

How to Handle Cookie in Php and set Cookie time in browser

Google+ Pinterest LinkedIn Tumblr

What Is Cookie?

Cookie is a small file embedded with server to the user’s browser. Its mainly used to Get or Identify the user details. Every time the same computer requesting a page with a browser, it will also send the cookie.
By locally we said, Its stored the pages and details in you browser History. you can check once the link in history. Until you cleared the history the webpage details are stored in you browser, you browser details are stored in the server with the help of cookie function.

Advantage of stored cookie is, If we browsing very large website or server in your browser its take large time to load for first time. But Second time its doesn’t take much time (send request to the server and get full website), it can easily access the website with the help of browser history which can previously stored using cookie.

How to Handle Cookie in Php and set Cookie time in browser

Using PHP you can create cookie function and set cookie and destroy cookie.

1. Create Cookies
A cookie is created with the setcookie() function.

setcookie(name, value, expire, path, domain, secure, httponly);

name (* required)   –   user name
value (* required)   –   user value
expire (optional)     –   can set expire time
path (optional)        –  url path
domain (optional)  –   server domain name

all are optionals

Example Code



Set cookie name, value and expire time to the page using setcookie() function
The cookie “Developerdesks” and “php” is stored in you browser as cookie

Real Time Checking
1. Run this code in your browser for first time the output is
“Cookie is not set set to the browser”
2.Refresh the same page again the output is
“Cookie ‘developerdesks’ is set!
Value is: Php”

Cookie “developerdesks” and “php” are stored in you browser history.

3. Delete the histor us CTRL+H -> clear browsing data -> Check “Cookies and other sites and plug-in data” and clear browsing data with the past hour.  now run the same page the output is

How to Handle Cookie in Php and set Cookie time in browser

“Cookie is not set set to the browser”

Because the cookie is deleted or expired. once you cleared history the cookie is deleted or expired.

If you not cleared the history its automatically cleared once the cookie set time is expired.

HINT : The setcookie() function must appear BEFORE the HTML tag or top of the page.

2. Modify the cookie.
We just create another user name and another setcookie function to modify the cookie values.


Output :

Cookie ‘user’ is set!       // Last Updated Cookie
Value is: Raja

3. Delete the Cookie
Just set expire time to delete the cookie


After one hour you reload the page the cookie is unset or delete

Cookie ‘user’ is deleted.

I'm Rajasekar - Web developer, Freelancer, Blogger and Owner of DeveloperDesks. From India lives in Bahrain. I love to do coding, Creating websites and trying different with code and designs. You Can Hire Me