Change language

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 = ;  
// 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 "
HTTP request successfully "
; else echo "
HTTP request not successfully! "
200 HTTP request successfully 


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Best laptop for engineering student


Best laptop for development


Best laptop for Cricut Maker


Best laptop for hacking


Best laptop for Machine Learning


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers


Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method