The “header()” function in PHP lets you manipulate headers, and this is most commonly how we do it when we need to set headers inside of a PHP script. Browsers give us a way of manipulating the headers they’re storing for the page, in the HEAD of a web document. We use it to set the character set of our newer web pages:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
This tells the browser to replace whatever it has for the Content-Type header and replace it with text/html; charset=UTF-8.
Finally, you can also manipulate headers in the .htaccess file of Apache servers. You can add or change them directly with the “header” directive. There are also directives for commonly-manipulated headers such as Content-Type (AddType) and Location (Redirect, RedirectPermanent, RedirectMatch).
AddType 'text/html; charset=UTF-8' .html
RedirectPermanent /as/ http://www.example.com/antstorm/
Header set Content-Type "text/html; charset=UTF-8"
Header add Timing "Hello Joe. It took %D microseconds for Apache to serve this request."
Header unset Location
For more information, see these pages:
http://php.net/manual/function.header
http://www.w3schools.com/TAGS/att_meta_http_equiv.asp
http://httpd.apache.org/docs/2.2/mod/mod_headers.html
- Manipulating headers: Redirects
- One of the most common manipulations is to redirect the visitor to another page. You might want to force them to always use the secure version of a page, or you might want to create a single, simple redirect page that redirects different types of visitors to different pages.
- Manipulating headers: Downloads
- We’ve already looked at one header manipulation: the downloads class sends headers that cause the browser to download the file instead of display it.
- Manipulating headers: Host
- On the client end: Host: www.hoboes.com Host: hoboes.com Host: www.godsmonsters.com Asking for http://www.hoboes.com/ is just like asking for http://216.92.252.156/; the client adds Host: www.hoboes.com (or www.godsmonsters.com) to let the server know which hostname is being requested. Before HTTP/1.1, every hostname had to have its own IP address.
- Filling out forms, getting a cookie
- In this part of the tutorial, we will log in to a remote server by sending the headers and body that get sent when a visitor fills out a form and submits it. We will then take the cookie that the remote server sends to the client on a successful login and use that cookie to maintain the session.
- Response codes
- Code