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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically