How to Create, Access and Delete Cookies in PHP


To set, retrieve and delete cookies in PHP. In this tutorial, you will learn how to set, get and delete cookies in PHP.

First of all, you need to know about cookies.

A cookie is a small file with the maximum size of 4KB that the web server stores on the client computer. Once a cookie has been set, all page requests that follow return the cookie name and value.

A cookie can only be read from the domain that it has been issued from. For example, a cookie set using the domain www.tutsmake.com can not be read from the domain career.tutsmake.com.

Note that, Each time the browser requests a page to the server, all the data in the cookie is automatically sent to the server within the request.

How to Create, Access and Delete Cookies in PHP

Use the following methods to set, get and delete cookies in PHP:

  • Set Cookie PHP
  • Get Cookie PHP
  • Delete Cookie PHP
  • Uses of PHP cookie

Set Cookie PHP

Let’s see the basic syntax of used to set a cookie in php:

<?php

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

?>

Example of set cookie in PHP:

$first_name = 'Tutsmake.com';
setcookie('first_name',$first_name,time() + (86400 * 7)); // 86400 = 1 day

Get Cookie PHP

To retrieve the get cookie in PHP:

<?php
     print_r($_COOKIE);    //output the contents of the cookie array variable 
?>

Output of the above code will be:

Output:

Array ( [PHPSESSID] => h5onbf7pctbr0t68adugdp2611 [first_name] => Tutsmake.com)

If you want to get only single cookie in PHP. So, you can use the key while getting the cookie in php as follow:

echo 'Hello '.($_COOKIE['first_name']!='' ? $_COOKIE['first_name'] : 'Guest'); 

Delete Cookie PHP

If you want to destroy a cookie before its expiry time, then you set the expiry time to a time that has already passed.

<?php

 setcookie("first_name", "Tutsmake.com", time() - 360,'/');

?>

Uses of PHP cookie

  • The cookie is a file websites store in their users’ computers.
  • Cookies allow web applications to identify their users and track their activity.
  • To set cookies, PHP setcookie() is used.
  • To see whether cookies are set, use PHP isset() function.

Conclusion

How to cookie set, retrieve and delete in php. In this tutorial, you have learn how to set, get and delete cookies in PHP. And as well as uses of PHP cookies.

Recommended PHP Tutorials

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?