PHP Cookies

Summary: in this tutorial, you’ll learn about cookies and how to use the PHP setcookie() function to manage cookies effectively.

Introduction to cookies #

The web works based on the HTTP protocol. The HTTP protocol is stateless.

When a web browser requests a page from a web server, the web server responds with the page content. Later, the same web browser requests the same page again, and the web server has no information that the request is from the same web browser.

Cookies solve this stateless challenge.

A cookie is a piece of data a web server sends to the web browser. The browser may store it and send it back in subsequent requests to the same web server. By using the same cookie, the web server knows that two requests come from the same web browser.

Cookies are also known as web cookies, HTTP cookies, or browser cookies. We’ll use the cookies to make it short.

The following flow chart illustrates how cookies work: