+

How to send HTTP response code in PHP?

For PHP 4.0:to send an HTTP response code, we need to collect a response code. To do this, use the header() function. The header() function contains a special use case that can detect an HTTP response string and replace it with a custom one.
header ("HTTP / 1.1 200 OK"); 
Example : $sapitype = php_sapi_name(); if ( substr ( $sapitype , 0, 3) == `cgi` ) header ( " Status: 404 Not Found " ); else header ( " HTTP / 1.1 404 Not Found " ); For PHP 4.3:there are obviously several problems with the first method. The biggest is that it is partially parsed by PHP and poorly documented. Since version 4.3, the header() function has an additional 3rd argument with which we can set the response code. but there must be a non-empty string to use the first argument.
header (`:`, true, 400); header (`X_PHP_Response_Code: 400`, true, 400); 
The second is recommended. The header field name in this version is not standardized and can be changed.For PHP 5.4:this version uses the http_response_code() function to make things easier.
http_response_code (400); 
Example:This example uses the http_response_code() function to send the HTTP response code. error_reporting (E_ERROR | E_PARSE);  
// Initialize the variable to the domain name $domain1 = ` https://contribute.engineerforengineer.org ` ;  
// Function for getting the HTTP response code function get_http_response_code ( $domain1 ) { $headers = get_headers ( $domain1 ); return substr ( $headers [0], 9, 3); }  
// function call $get_http_response_code = get_http_response_code ( $domain1 );  
// Show HTTP response code echo $get_http_response_code ;  
// Check the HTTP 200 response code or not if ( $get_http_response_code == 200) echo " < br > HTTP request successfully " ; else echo " < br > HTTP request not successfully! " ;  
?>
Output:
200 HTTP request successfully 
Get Solution for free from DataCamp guru