Skip to content

Commit 1d5403e

Browse files
committed
Adding information about headers in the IncomingRequest docs
1 parent 3610f1d commit 1d5403e

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

user_guide_src/source/libraries/incomingrequest.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,46 @@ Filtering a POST variable would look like this::
146146
All of the methods mentioned above support the filter type passed in as the last parameter, with the
147147
exception of ``getJSON()``.
148148

149+
Retrieving Headers
150+
==================
151+
152+
You can get access to any header that was sent with the request with the ``getHeaders()`` method, which returns
153+
an array of all headers, with the key as the name of the header, and the value being an instance of
154+
``CodeIgniter\HTTP\Header``::
155+
156+
var_dump($request->getHeaders());
157+
158+
[
159+
'Host' => CodeIgniter\HTTP\Header,
160+
'Cache-Control' => CodeIgniter\HTTP\Header,
161+
'Accept' => CodeIgniter\HTTP\Header,
162+
]
163+
164+
If you only need a single header, you can pass the name into the ``getHeader()`` method. This will grab the
165+
specified header object in a case-insensitive manner if it exists. If not, then it will return ``null``::
166+
167+
// these are all equivalent
168+
$host = $request->getHeader('host');
169+
$host = $request->getHeader('Host');
170+
$host = $request->getHeader('HOST');
171+
172+
You can always use ``hasHeader()`` to see if the header existed in this request::
173+
174+
if ($request->hasHeader('DNT'))
175+
{
176+
// Don't track something...
177+
}
178+
179+
If you need the value of header as a string with all values on one line, you can use the ``getHeaderLine()`` method::
180+
181+
// Accept-Encoding: gzip, deflate, sdch
182+
echo 'Accept-Encoding: '.$request->getHeaderLine('accept-encoding');
183+
184+
If you need the entire header, with the name and values in a single string, simply cast the header as a string::
185+
186+
echo (string)$header;
187+
188+
149189
The Request URL
150190
===============
151191

0 commit comments

Comments
 (0)