PHP Web Fundamentals
Writing an HTTP Header
Problem
You want to write an HTTP header.Solution
Call the header() function:// Tell 'em its a PNG
header('Content-Type: image/png');
Discussion
Your web server and PHP often take care of setting all the necessary headers with the proper values to serve your script. For example, when you return an HTML page, the Content-Length or Transfer-Encoding header is automatically set to let the browser know how to determine the size of the response.The header() function lets you explicitly set these values when there’s no way for the server to compute them or you want to modify the default behavior.
For instance, many web servers are configured to send a Content-Type header of text/ html for all pages processed by PHP. To also use PHP to create a JSON file, one option is changing the Content-Type from within your script itself:
header('Content-Type: application/json');
If you set the same header multiple times, only the final value is sent. Change this by passing true as the second value to the function:
header('WWW-Authenticate: Basic realm="http://server.example.com/"');
header('WWW-Authenticate: OAuth realm="http://server.example.com/"', true);
When you support multiple ways for someone to authenticate himself, it’s okay to return multiple WWW-Authenticate headers. In this case, someone can either use HTTP Basic authentication or OAuth.
No comments:
Post a Comment