PHP and MySQL (56 Blogs) Become a Certified Professional

What is a cookie in PHP?

Published on Nov 07,2019 6.6K Views

What is a cookie in PHP?

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.

What is a Cookie?

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 −

  • The server sends a set of cookies to the browser. 
  • The browser stores this information on the local machine for future utilization.
  • When the next time the client’s computer sends any request to the webserver then it sends that cookie information to the server and the server uses that information to identify the user.

Cookies-in-HTML

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]);
  • “Name” is mandatory; It specifies the name of the cookie that the server uses while retrieving its value from the array $ _COOKIE.
  • “Value” is mandatory; it sets the value of the named variable and it is the content that you actually want to store.
  • “[Expiry_Time]” is optional; it is used to define the expiry time for the cookie. The time is set using the PHP time() functions plus or minus a number of seconds greater than 0 i.e. for 1 hour it will be – time() + 3600 and for one day it will be – time() + 86400. The default Expiration Period for a Cookie is 30 minutes.
  • “[Cookie_Path]” is optional; it is used to set the cookie path on the server. The forward slash “/” means that the cookie will be made available on the entire domain. Subdirectories limit the cookie access to the subdomain.
  • “[Domain]” is optional. This is used to specify the domain name in very large domains and must contain at least two periods to be valid. All cookies are only valid for the host and domain which created them.
  • “[Secure]” is optional. It is used to determine whether the cookie is sent via https if it is set to true or HTTP if it is set to false. The default is set to false.

PHP Create/Retrieve a Cookie

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:
Output - EdurekaView - PHP Cookies - Edureka

Modify a Cookie Value

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.
Cookie modify - Edureka

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.

Output - PHP Cookies - EdurekaDelete a Cookie

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:
Delete PHP cookie - Edureka

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.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is a cookie in PHP?

edureka.co