In PHP, how do you send HTTP headers from a script, and which built in function is used to add or modify an HTTP header such as a Location redirect?

Difficulty: Easy

Correct Answer: By calling the header function, for example header("Location: /somepage.php") before any output is sent

Explanation:


Introduction / Context:
When building web applications in PHP, you often need to send HTTP headers to control caching, content type, redirects, or cookies. PHP provides a simple way to set these headers programmatically from within the script. This question tests whether you know which function is used and how it must be called in order to work correctly.


Given Data / Assumptions:

  • The environment is a standard PHP enabled web server.
  • You want to set or modify HTTP headers, such as Location or Content Type.
  • The focus is on adding headers from PHP code, not from server configuration files.
  • We assume that the script is allowed to send headers and that output buffering rules apply.


Concept / Approach:
PHP uses the header function to send raw HTTP headers to the client. This function must be called before any body output is sent, unless output buffering is enabled. Common uses include redirecting a user to another page by sending a Location header, specifying the MIME type with a Content Type header, or controlling caching behaviour. The key points are calling header with the exact header string and ensuring that no unintended output precedes it.


Step-by-Step Solution:
1. Remember that the PHP manual describes header as the function that sends raw HTTP headers. 2. To perform a redirect, you typically call header with a Location header, indicating the target URL. 3. Ensure that the header function is called before any echo statements or HTML output, so the server can still modify the response headers. 4. After sending a redirect header, it is good practice to stop script execution to avoid sending unwanted content. 5. This standard usage confirms that header is the correct function to add or modify HTTP headers from a PHP script.


Verification / Alternative check:
You can verify this behaviour by creating a simple PHP script that calls header to set a custom header or redirect and then inspecting the response using browser developer tools or a command line client. You will see the custom header in the response. If any output is sent before the header call and output buffering is disabled, PHP will usually produce a warning and the header may not be sent, which demonstrates the importance of calling header early.


Why Other Options Are Wrong:

  • Option B is wrong because simply echoing text does not create real HTTP headers; once output begins, headers must already have been sent.
  • Option C is wrong because writing header lines to a text file does not instruct the web server to use them for responses.
  • Option D is wrong because php.ini is a configuration file and is not meant to be edited for each individual request to change headers.


Common Pitfalls:
A frequent mistake is attempting to call header after some output has already been sent to the browser, which leads to the cannot modify header information warning. Another pitfall is forgetting to exit after sending a redirect, which might cause unwanted output to appear. Developers should also ensure that header values are correctly formatted and not influenced by user input in a way that could introduce header injection vulnerabilities. Understanding these details leads to correct and secure use of the header function.


Final Answer:
The correct explanation is By calling the header function, for example header("Location: /somepage.php") before any output is sent, because header is the standard PHP function used to send HTTP headers from scripts.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion