Skip to content

Http

Overview

A HTTP transaction is made up of a request and a response. The request line will contain the method the path an the http version. The response line will contain the http version, the response code and the english phrase for the code.

Each transaction can also have headers and a body. Headers are information about the request/ response and the body is a body of data sent after the header lines. This is most commonly the resource in responses.

Working with headers in PHP

Headers are simple to work with in php:

<?php

header('x-easy-header: this simple!');

header_remove('x-easy-header');

headers_list(); # Lists the sent/ to be sent headers

headers_sent(); # Checks if or where header have been sent.

All headers must be sent before any output including blank lines.

Cookies

A cookie is a type of header, to save us reinventing the wheel when working with cookies php has the setcookie() method.

This lets you set the name, value, expiration and other cookie specific options, read the docs for full usage of the method.

There is also setrawcookie() which is the same as setcookie() but doesn't automatically url encode the cookie.

A session is data stored for a specific user on the server side. This is often identified by a ‘session cookie’.

Reference:

https://www.php.net/manual/en/function.header.php

https://www.php.net/manual/en/function.header-remove.php

https://www.php.net/manual/en/function.headers-list.php

https://www.php.net/manual/en/function.headers-sent.php

https://www.jmarshall.com/easy/http/

https://en.wikipedia.org/wiki/List_of_HTTP_header_fields