header_register_callback is a rarely discussed PHP function that allows you to register a callback function to modify or append HTTP headers before PHP sends them. This means you can perform custom operations before outputting headers, including setting response status codes or adding custom headers.
Function Prototype:
<span><span><span class="hljs-title function_ invoke__">header_register_callback</span></span><span>(</span><span><span class="hljs-keyword">callable</span></span><span> </span><span><span class="hljs-variable">$callback</span></span><span>)
</span></span>
$callback: A callable function that is executed before the response headers are sent. This callback receives an array parameter containing all current HTTP headers.
This approach gives you flexible control over the headers, ensuring adjustments are made before the response is sent.
http_response_code is a PHP function used to get or set the HTTP response status code. By default, PHP sets the status code to 200 (OK) when processing a request. However, if you want to return a different status code, you can use http_response_code to modify it.
Function Prototype:
<span><span><span class="hljs-title function_ invoke__">http_response_code</span></span><span>(</span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$code</span></span><span> = </span><span><span class="hljs-literal">NULL</span></span><span>)
</span></span>
$code: The HTTP status code to set. If omitted, the function returns the current status code.
Common HTTP status codes include:
200: OK (Request succeeded)
404: Not Found
500: Internal Server Error
403: Forbidden
By combining header_register_callback and http_response_code, you can easily set custom HTTP status codes. Here is a simple example demonstrating how to dynamically set the HTTP status code based on different conditions.
Example Code:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Register a callback function to modify HTTP headers</span></span><span>
</span><span><span class="hljs-title function_ invoke__">header_register_callback</span></span><span>(function() {
</span><span><span class="hljs-comment">// Check conditions and set different HTTP status codes accordingly</span></span><span>
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-variable">$_SERVER</span></span><span>[</span><span><span class="hljs-string">'REQUEST_METHOD'</span></span><span>] === </span><span><span class="hljs-string">'POST'</span></span><span>) {
</span><span><span class="hljs-title function_ invoke__">http_response_code</span></span><span>(</span><span><span class="hljs-number">201</span></span><span>); </span><span><span class="hljs-comment">// Created successfully, return 201 status code</span></span><span>
} </span><span><span class="hljs-keyword">elseif</span></span><span> (</span><span><span class="hljs-variable">$_SERVER</span></span><span>[</span><span><span class="hljs-string">'REQUEST_METHOD'</span></span><span>] === </span><span><span class="hljs-string">'DELETE'</span></span><span>) {
</span><span><span class="hljs-title function_ invoke__">http_response_code</span></span><span>(</span><span><span class="hljs-number">204</span></span><span>); </span><span><span class="hljs-comment">// Deleted successfully, no content returned, return 204 status code</span></span><span>
} </span><span><span class="hljs-keyword">else</span></span><span> {
</span><span><span class="hljs-title function_ invoke__">http_response_code</span></span><span>(</span><span><span class="hljs-number">200</span></span><span>); </span><span><span class="hljs-comment">// Default return 200 status code</span></span><span>
}
});
<p></span>// Output response content<br>
echo "Response content...";<br>
?><br>
</span>
In the code above, we first register a callback function to set the HTTP status code before PHP sends the headers. Based on the request method, we dynamically return different HTTP status codes:
For POST requests, return 201 Created to indicate the resource was successfully created.
For DELETE requests, return 204 No Content to indicate successful deletion with no content returned.
For other requests, return the default 200 OK status code.
When using header_register_callback, pay attention to the following:
Call Order: The callback registered with header_register_callback executes before any headers are sent, ensuring that header modifications (including status codes) are applied before the response is sent.
Sending Headers: When calling http_response_code, ensure it is done before any output (HTML or text) is sent. Otherwise, PHP will issue a warning because headers must be sent before output.
Compatibility: header_register_callback is rarely used, and some PHP versions or configurations may not support it. Always perform compatibility testing in production environments.
By combining header_register_callback and http_response_code, PHP developers can easily return custom HTTP status codes based on different business logic. This improves the flexibility of APIs and allows front-end or client applications to handle responses according to the status code, enhancing maintainability and user experience.
With the guidance in this article, you should now have a clearer understanding of how to use these two functions to efficiently control HTTP headers and status codes in real-world development.