Introduction

To detect a user, a cookie is used frequently . A cookie is a tiny file placed on the user's machine by the server. The cookie will be sent each time the same machine requests a page via a browser. Cookie values can be created and retrieved using cookies in PHP.

Cookies are text files that are saved on the client computer for the purpose of monitoring. PHP accepts HTTP cookies invisibly.

Benefits of Cookies

All of the data in the cookie is automatically sent to the server each time the browser requests a page from the server.

Cookies Operations

Returning users are identified using a three-step process applied for cookies in PHP.

  • A collection of cookies is sent to the browser by the server script. Name, age, or identification number, for example. 
  • This information is saved on the local computer by the browser for future use.
  • When the browser sends a request to the web server the next time, it sends those cookies information to the server, which the server uses to identify the user.

When to Use Cookies

  • Cookies allow us to monitor the state of the application using small files stored on the user's computer because HTTP is a stateless protocol.
  • The location of the cookies is determined by the browser.
  • They're normally saved in the Temporary Internet Files folder in Internet Explorer.
  • Allowing users to choose their preferences allows for a more personalized user experience.
  • The page you requested was not found.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

The Anatomy of Cookies in PHP

In most cases, cookies are set in the HTTP header (although JavaScript can also set a cookie directly on a browser). It generally shows the location path of the cookies in PHP. If a PHP script sets a cookie, it can submit headers that look like this:

HTTP/1.1 200 OK

Date: sat, 24 April 2021 21:03:38 GMT

Server: Apache/1.3.9 (UNIX) PHP/4.0b3

Set-Cookie: name=xyz; expires= sat, 24 April 2021 21:03:38 GMT;

Path=/; domain=localhost

Connection: close

Content-Type: text/html

The Set-Cookie header, as you can see, contains a name value pair, a GMT date, a path, and a domain. URL encoding will be used for the name and value. The expires field tells the browser that the cookie should be "forgotten" after the specified time and date. If the browser is set to save cookies, the details will be saved until the expiration date.

If the user navigates to some page on the internet.

Cookies_in_PHP_1.

How to Create Cookies in PHP

The setcookie() function is used to generate a cookie.

Syntax:

setcookie(cookie_name, cookie_value,date_of_ expire,cookie_ path,Web_ domain,      protocol_security);

Cookie_name:

This sets the cookie's name, which is saved in the HTTP COOKIE VARS environment variable. When accessing cookies, this variable is used.

Cookie_value:

This is the content that you really choose to store and sets the value of the named variable.

Date_of_expire:

This specifies a future time in seconds.After this time, the cookie will no longer be available.

If this parameter is not set, cookies will expire when the Web Browser is closed.

cookie_path:

This indicates which folders the cookie is valid for. The cookie is true for all directories thanks to a single forward slash character.

web_domain:

This can be used to specify a domain name in very broad domains, and it must include at least two cycles in order to be legitimate. Cookies are only valid for the host and domain from which they were generated.

protocol_security:

This can be set to 1 to indicate that the cookie can only be transmitted via secure HTTPS transmission, or 0 to indicate that the cookie should be sent via standard HTTP.

Sample Code

setcookie("first_cookie", "username_of_the account is anon", time()+3600, "d:/testcoookie/","", 0);

First cookie located here, is the name of the cookie that stores the meaning as the username of the account is anon and the time between logged in and 3600 milliseconds. Following that, it will be deleted from the screen.

D:/testcookie/- location of cookies which store the cookie value in a specified amount of time.

 Code

Cookies_in_PHP_2.

Explanation

  • In the above example, two cookies have been created. One is the first_cookie used to hold the name of the user .
  • Second is the second_cookie used to store the age of the user.
  • Both the cookies will be saved into the location d:/testcookie/ on the local machine.
  • The cookie will expire after 3600 milliseconds when it starts to be kept on the local machine.
  •  That zero represents the HTTP.

How to Access the Cookies in PHP

Cookies can be accessed in a variety of ways in PHP. The most straightforward method is to use the $_COOKIE or $HTTP COOKIE VARS variables. It's usually an associative array that's keyed by cookie name and includes a list of all the cookie values sent by the browser in the current request.  The example below will access all of the cookies set in the previous example.

Sample Code

      echo $_COOKIE["first_cookie"]. "<br />";

      echo $HTTP_COOKIE_VARS["second_cookie"];

Code

Cookies_in_PHP_3

Check Cookies

The isset() function can be used to determine whether or not a cookie has been set.

Code:

Cookies_in_PHP_4.

Output:

Set Cookies    Welcome anon

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Deleting the Cookies in PHP

Officially, you can call setcookie() with only the name argument to delete a cookie, however, this does not always function well and should not be depended upon.

Code:

Cookies_in_PHP_5 

To reset the time as subtraction timing in the example above. It deletes cookies from the machine automatically.

Cookie 'first_cookie' is deleted.

Conclusion

To recognize a user, a cookie is frequently used. A cookie is a tiny file placed on the user's machine by the server. The cookie will be sent each time the same machine requests a page via a browser. Cookie values can be created and retrieved using PHP.

Hope you found the information in this article useful. Become a master of modern coding techniques with Simplilearn’s Postgraduate Program in Full Stack Web Development that is conducted in partnership with Caltech CTME. Joining this course will help you advance your career as a software developer, and you'll have everything to become a full-stack technologist.

As a support during the difficult times, we have also put our courses together for free! You can also gain access to our free courses and enhance your current skill set. There is no prerequisite for any course. Just your interest and passion is enough. 

Just browse through the courses and if you have any questions, leave them in the comments section of this article, and our experts will get back to you on them, as soon as possible!