Sending Cookie in Request Header [PHP Code]

To send cookies to the server in the request header using PHP, you need to add the "Cookie: name=value" HTTP header to the request. To send multiple cookies in one Cookie header, you must separate them with semicolons. Servers store cookies in the client browser by returning "Set-Cookie: name=value" HTTP headers in the response. In this PHP Send Cookies Example, we send cookies to the ReqBin echo URL in the HTTP request header. Click Send to execute PHP Send Cookies Example online and see the results. The PHP code was automatically generated for the Send Cookies example.
Sending Cookie in Request Header [PHP Code] Send
GET /echo HTTP/1.1
Host: reqbin.com
Cookie: name=value; name2=value2

Updated: Viewed: 54750 times
PHP code for Send Cookies example

PHP code for Send Cookies Example

This PHP code snippet was generated automatically for the Send Cookies example.
<< Back to the Send Cookies example

What is HTTP?

The Hypertext Transfer Protocol is the data transfer protocol that underlies the World Wide Web. HTTP transfers data between HTTP clients (browsers and mobile apps) and servers. HTTP is based on a client-server architecture. HTTP works as a request-response protocol between client and server. The client initiates a connection and sends a request. The server receives the request, executes it, and sends the result to the client.

What is Cookie?

HTTP Сookies are small pieces of text data sent by a website and stored on a user's computer by a web browser. The data stored in the cookie is created by the server when it processes a request from the client. This data is tagged with an identifier that is unique to you and your computer. The browser sends cookies back to the server with each subsequent request. The server reads the identifier from the cookie and determines whether the request came from the same browser or not. After that, the server knows exactly what information can be provided to the user (based on user permissions). Cookies are mainly used to manage user sessions, track users, store user preferences and site settings.

HTTP Cookies Header Syntax

Following is the syntax for the Cookie header:

Cookies Header Syntax
Cookie: [cookie-name=value]

Why use Cookies?

Cookies are mainly used for three purposes:

  • Session management: Logins, passwords, or whatever the server needs to remember
  • Personalization: Custom settings, themes, and other settings
  • Tracking: Recording and analysis of user behavior

How to see saved Cookies in the browser?

To see saved Cookies in your browser:

  1. Navigate to a website;
  2. Press F12 to open the developer tool;
  3. Click the Application tab;
  4. In the tree in the Storage section, select Cookies and your website address;
  5. Here, you will see all the Cookies stored for your browser for this site.

How to send Cookies?

The following is an example of sending a cookie header to the ReqBin echo URL:

Send Cookies Example
GET /echo HTTP/1.1
Host: reqbin.com
Cookie: name=value; name2=value2

Response to our example of sending Cookies:

Cookies Response Example
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Set-Cookie: ... domain=.reqbin.com;
    

See also

Generate code snippets for PHP and other programming languages

Convert your Send Cookies request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.