Set and Get Cookies in PHP 7

In this article we will Set and Get Cookies in PHP 7. The cookie is very important in PHP to store temporary data of the user.  In this article, we are creating cookies using PHP and will also show you how you can get and set cookies. now we first wanted to know what is cookies?

We will explain cookies in very simple definition, Cookies are just the data that store in the client Browser on each site visit. The server can retrieve the data to identify the user or to track the user what he is doing on the web application. So to track the user we are using session and cookie. we are not combining station in this article because this article becomes more complex.

In the subsequent article, you will learn how to create sessions and use their data in the web application. Now in this article, we just want to focus on cookies. In this article will understand how to set cookies in the client browser and how you can get those cookies and store it in the PHP variable.

let’s take a look at how to Set and Get Cookies.

Set Cookies

Before we set and create cookies in the client browser the HTTP protocol provides some minimum requirement to create cookies.  so the first requirement is Your browser must support at least 50 cookies per website. Then the second requirement is your browser must able to store at least 3000 cookies total Until the last requirement is Your browser must support cookies up to 4,094 bytes in size.

The following is the basic format of setcookies() function.

The only required parameter of cookies is named. If you don’t specify anything in the optional parameter it will return null.

Now I just wanted to explain what optional parameter takes. Just after the name parameter, you will see the string value so you just wanted to specify the value for the cookies. Thr expire parameter allows you to specify expiration time to the persistent cookies.  the next four parameters are for Specifying URL path And domain love you to access the cookies. And the last parameter checks whether the cookies are set as secure on HTTP only.

So let’s take a look at the example


In the above code be specified set cookies function at the top of the page. You can specify this function anywhere you want.

In the set cookies function we first specify the name of the cookies test then we specify value Jonathan and then we specify the expiration time of that cookies so we get time from the system.

Set and Get Cookies

Now I just wanted to show you where you can find this cookie.  run the sample that sample.php file. right click on the document and select Inspect.  find out application tab and click on it. there is a cookie section over there click on the cookies which you recently created.

that’s it now you can see your cookie name and cookie value with expiry.


Get Cookies

Getting two values of cookies is very simple. You just need to check if the cookies are set or not using the isset() function. If you don’t do that then the error will be thrown if the cookies not created. Then we just call the cookies with the name of the cookies. So this will return the value of your cookies.

check out the previous example again and update it with the following code.

Now in this code, we first check if the cookies set or not if it is set then be display the value of the cookies otherwise will return the error message.

so when you run this file you will get the value of your cookies test variable.

That’s it now your cookies is ready.  Now you are able to Set and Get Cookies for your web application. In the next article will learn about the session in PHP.  Sessions are more important in PHP to identify and track the user we’ll talk about the session in the next article.