You are currently viewing Cookies in PHP?
How to set Cookies in PHP program?

Cookies in PHP?

Please Don’t compare to your mom’s cookies, it’s a web Cookie. We know that Cookies play an important role in web pages and stored in the client machine. Cookies serve the client’s data to your intended server. These Cookies are sent by HTTPS Headers. Don’t save private important credentials in the Cookies, because it’s not safe.

Let’s assume that you have a file and store your internet banking’s user-id and password without encryption mode, is it safe or not? Obviously – ‘Not’. In General Term, Cookies are used to store the server’s basic information such as the last visit on the server’s website, some important authentication keys from which the server will know who you are? and many different types of server data, Ok.

This is a brief description of Cookies? So now, we are going to see in this post – What are Cookies? How to set the Cookies in our PHP Program? How to read the Cookies from web pages when we needed? How to set the expiration time of Cookies? How to delete the Cookies?

What is Cookie in PHP?

This topic is of central interest to PHP programmers, so this post is going to be an interesting one. We know that the Internet is a stateless place – that is, Web Pages don’t store data.let’s see, if a person visits the website the first time and after the visiting, close the website. now, the second time, the person again visit that website, there is no change on that website and you will see the same as the first time, the person visit.

The internal process happens when the user visits the website each time without existing Cookies – the web page reinitialized and is displayed anew.

Internal process happens when the user visits the website each time with existing Cookies – Cookies are fetched by the server and the web page is displayed on behalf of the saved Cookies data.

However, the web has become a more serious place in terms of programming, and that means writing the Web Applications that work in a multi-page way: that means storing the data about the user that persists from page to page and that user’s data is known as Cookies.

How to set the Cookies in PHP?

Cookies are the text segments that we can store on the user’s computer, and PHP has the ability to do set and read the Cookies perfectly using pre-defined PHP functions. Those segments of text can persist even when the user’s computer is turned off, so we can store information about the user easily. This is a good thing from which the user can customize the web application using the saved cookies.

Cookies are used for all kinds of purposes, from the benign to the more sinister — such as tracking what ads a user has already seen and responded to. Setting and Reading Cookies in PHP is not Hard. You set cookies on the user’s machine with the PHP setcookie() function:

setcookie(name[,value[,expire[,path[,domain[,secure]]]]]) 

Here is what the parameters mean:

  • name: The name of the cookie.
  • value: The value of the cookie. (This value is stored on the client’s computer, so do not store sensitive information.)
  • expire: The time The Cookie expires. This is the number of seconds since January 1, 1970. you’ll most likely set this with the PHP time function plus the number of seconds before you want it to expire.
  • path: The path on the server in which the cookie will be available on.
  • domain: The domain for which the cookie is available.
  • secure: This indicates that the cookie should only be transmitted over a secure HTTPS connection. When set to 1, the cookie will only be set if a secure connection exists. The default is 0.

Ok. we will see a really simple cookie example and after we will use in the PHP program.

This example sets a cookie named “name” and its value “Prajjwal”

<?php setcookie("name","Prajjwal");?> //set the cookie on the client machine.
//let's go and use the above function - [setcookie()]
 in the below example. //  
filename: setcookie.php

 <html>
 <head> 
<title> Setting a Cookie </title>
 <?php setcookie ("name","Prajjwal");
 ?>
 </head>
 <body>
 <h1>Set the Cookie</h1>
 The Cookie was set. Go to <a href="getcookie.php">GetCookie </a> to read it.
 </body>
 </html>

How to read the Cookies in PHP?

when we set a cookie, it won’t be visible to our scripts until the next time we load a page. That’s because the cookie is sent to the server from the user’s machine using the above program. So immediately after you set a cookie, you won’t be able to read it; you need to get a page back from the browser first.

Note also that cookies are sent in the HTTP headers in pages sent to you by the browser, and if your cookie-handling page is in domain A such as (www.bittutech.com), only the cookies came from domain A are sent to you. Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array. we set a cookie named “name” in the above paragraph and we’ll read it here. The values of cookies are automatically loaded into the global array named $_COOKIES, such as the value of web page data are stored in $_REQUEST, which makes this process easy.

Let’s see an example to How to read the cookies in PHP.

//File Name: getcookie.php      
<html>
      <head>
      <title>
      Reading the saved Cookie
      </title>
      </head>
      <body>
      <h1>Reading a Cookie</h1>
     The Cookie is : 
     <?php
       if(isset($_COOKIE['name']))
       {
       echo $_COOKIE['name'];
       }
     ?>
      
      </body>
      </html>   

How to set the expiration time for Cookies in PHP?

Ok that lets you set cookies and read them. However, this cookie will be deleted as the user closes their browser. We can set Cookie’s expiration time when we create the cookie with the setcookie() function.
setcookie(name[,value[,expire[,path[,domain[,secure]]]]]);

here, we can use php time() function which is used to get the current time of the system. so, if we want to set the cookie expiration time for 1 hour, so the value of parameter “expire” is – time()+3600, we have to use expire parameter time value in the form of seconds such as 1 hour = 60*60=3600(as above mentioned).

Let’s see an example for more clarification.

<html>
<head>
<title> Setting a Cookie </title>
<?php setcookie ("name","Prajjwal",time()+3600);
?>
</head>
<body>
<h1>Set the Cookie</h1>
The Cookie was set for 1 hour. Go to <a href="getcookie.php">GetCookie </a> to read it.
</body>
</html>

Note – Copy the code and paste it on your editor for more clarity.

How to delete the cookies in PHP

We can delete the Cookies using edit our program because there is no specific PHP function for deleting the cookies, when we don’t want to keep the particular website’s cookies on our local machine. Sometimes, we accidentally open a Harmful website and also give ‘Allow’ permission to their website cookies. it can be harmful. so we can delete the cookies from in the browser settings. but if we make our own website and want to delete cookies, so how it is possible?

Let’s more understand with an example –

<html>
<head>
<title> Setting a Cookie </title>
<?php setcookie ("name","Prajjwal",time()-3600);
?>
</head>
<body>
<h1>Set the Cookie</h1>
The Cookie was deleted. check that a <a href="getcookie.php">GetCookie </a>.
</body>
</html>

Note – Copy the code and paste it on your editor for more clarity.

Share post

Prajjwal Singh

Tech Blogger || Web developer || Computer Networking Enthusiast || Microsoft SQL Database Management Expert || Software Debugger || Learned DOS OS Structure

This Post Has One Comment

Leave a Reply