Cookies are key/value pairs, that are sent to the browser along with
some other information, such as which paths the cookie is valid for and when it expires.
Since PHP is designed to solve “the Web problem,” it has some great features for working
with cookies. To set a cookie, use a function helpfully called setcookie():
<?php
setcookie("visited", true);
We can use this approach to show a welcome message to a visitor when he first comes
to the site—because without any previous cookies, he won’t have the “visited” cookie
set. Once he has received one response from this server, his “visited” cookie will be seen
on future requests. In PHP, cookies that arrived with a request can be found in the
$_COOKIEsuperglobal variable. It is an array containing the keys and values of the cook‐
ies that were sent with the request.
When working with APIs, the same facilities are available to us. When PHP is a server,
the techniques of using setcookie and checking for values in $_COOKIEare all that are
needed, exactly like when we are working with a standard web application. When con‐
suming external services in PHP, it is possible to send cookie headers with our requests
in the usual way.