What is a cookie in PHP?

Published on Nov 07,2019 7K Views

What is a cookie in PHP?

edureka.co

A cookie in PHP is a small file with a maximum size of 4KB that the server embeds on the client’s computer. In this article, you will learn all about PHP cookies along with its various operations such as create, retrieve, modify etc. 

Following Pointers will be covered in this article:

Let’s begin.

A cookie is a small file with a maximum size of 4KB that the server embeds on the client’s computer. It is often used to identify a user.  Whenever the same computer requests a page with a browser it also sends the cookie. With the help of PHP, We can both create and retrieve cookie values. PHP transparently supports HTTP cookies.

There are three steps involved in identifying returning users −

Create Cookies With PHP

PHP provided setcookie() function to set a cookie. This function requires up to six arguments. For each cookie, this function has to be called separately.

Syntax:

setcookie(Name, Value, [Expiry_Time], [Cookie_Path], [Domain], [Secure]);

Let us create a file “cookie.php” and write the following code to set the value of cookie:

Code:

<?php //setcookie(name, value, expire); setcookie('actor_name', 'joaquin phoenix', time()+30); setcookie('age', '44', time()+30); echo "The cookie has been set for 30 seconds"; ?>

 

Now let us create a new file “view_cookie.php” and write the following code to retrieve the value of the cookie, to retrieve the value of a set cookie we use $_COOKIE array variable.


<?php $actor_name = $_COOKIE['actor_name']; $actor_age = $_COOKIE['age']; echo "$actor_name"; echo " $actor_age"; ?>

Output:

To modify a cookie we just need to set its value again using setcookie() function:

Code:

<?php $cookie_name = "actor_name"; setcookie('actor_name', 'robert de niro', time()+30); ?>
<html>
<body>
<?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named ' . $cookie_name . ' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set! "; echo "actor name is: " . $_COOKIE[$cookie_name]; } ?>
</body>
</html>

Output:

First, when we load the cookie_modify file it will print “Cookie named ‘actor_name’ is not set!” since only after the first run setcookie function is called.

After refreshing the page it will set the cookie named “actor_name” for 30 seconds and save the string ‘robert de niro’ as the cookie value.

To delete a cookie value, we can simply use the setcookie() function and set the expiry time of the cookie in the past. In the following snipet we have set the expiration date to one hour ago  

<?php $cookie_name = "actor_name"; setcookie('actor_name', 'robert de niro', time()-3600); echo "Cookie named '" . $cookie_name . "' is deleted"; ?>

Output:

What is a Session?

A simple HTML website does not pass data from one page to another. In other words, all information is lost when a new page is loaded. Session solves this problem by allowing us to store the information on the server which we can use later on. 

A session can be defined as a global variable stored on the server. Each session is assigned a unique id that is used to retrieve stored values from the server. Whenever a session is initiated, a cookie containing the unique session id is stored on the user’s system and returned with every request to the server. Unique PHP session-id is displayed in the URL if the client browser does not support cookies. Sessions can store relatively large data compared to cookies. When the browser is closed the session values are automatically deleted. To store the values permanently, we should store them in the database. Similar to the $_COOKIE array variable, session variables are stored in the $_SESSION array variable and just like cookie the session must be started before any HTML tags.

Now with this, we have come to the end of the PHP Tutorial. I hope you guys enjoyed this article and understood the concepts of PHP. So, with the end of this PHP Tutorial, you are no longer a newbie to the scripting language.

If you found this PHP Tutorial blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of  “Cookies in PHP ” and I will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework