PHP How To Work with Cookie Set Get And Delete Tutorial

Share this Article
Reading Time: 4 minutes
566 Views

Inside this article we will see the concept i.e PHP How To Work with Cookie Set Get and Delete. Article contains the classified information about Working with PHP cookie to store value, get value and destroy value.

Cookies are small pieces of text sent to your browser by a website you visit. They help that website remember information about your visit, which can both make it easier to visit the site again and make the site more useful to you.

Learn More –

Let’s get started.

What is Cookie in PHP?

A cookie in PHP is a small file with a maximum size of 4KB that the web server stores on the client computer. They are typically used to keep track of information such as a username that the site can retrieve to personalize the page when the user visits the website next time.

  • Cookies do not require any server resources since they are stored on the client.
  • Cookies are easy to implement.
  • You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).

How To Store Value in Cookie

To store value in cookie we will use a PHP function called setcookie().

Syntax

setcookie(cookie_name, cookie_value, [expiry_time], [cookie_path], [domain], [secure], [httponly]);

Example

Let’s say we have a file called index.php. Open file and write this following code into it.

<?php

$welcome_message = 'Welcome To Online Web Tutor';

setcookie('owt_welcome', $welcome_message, time() + (86400 * 7)); // 86400 = 1 day

When we execute, it will store cookie value into browser with key name owt_welcome

You can find cookie value as –

How To Get Value From Cookie

To get browser cookie value, simply you need to use PHP global variable $_COOKIE.

<?php
  
if (!isset($_COOKIE["owt_welcome"])) {

    echo "Cookie not found!";
} else {
    
    echo $_COOKIE["owt_welcome"];
}  

If cookie exists with the given key, then it returns the value of cookie.

To print all cookies,

print_r($_COOKIE);

How To Destroy a Cookie value

To delete a cookie, use the setcookie() PHP function with an expiration date in the past.

<?php
  
// set the expiration date to one hour ago
  
setcookie("owt_welcome", "", time() - 3600);

We hope this article helped you to learn PHP How To Work with Cookie Set Get And Delete Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4