Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom headers in PHP

JAVA developer send me data in headers. I take it this way

$_SESSION["HTTP_COUNTRYNAME"];

How to make response back with headers? It tried header("countryname: USA"); but php function headers_list doesn't show it.

like image 443
Dimash Avatar asked Sep 04 '11 14:09

Dimash


People also ask

What PHP can do with header () command?

The header() function is an predefined PHP native function. With header() HTTP functions we can control data sent to the client or browser by the Web server before some other output has been sent. The header function sets the headers for an HTTP Response given by the server.

How can I get header in PHP?

The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request. Parameters: This function accepts three parameters as mentioned above and described below: $url: It is a mandatory parameter of type string.

How can you send an HTTP header to the client in PHP?

PHP | header() Function. The header() function is an inbuilt function in PHP which is used to send a raw HTTP header. The HTTP functions are those functions which manipulate information sent to the client or browser by the Web server, before any other output has been sent.

What is header content-type in PHP?

The Content-Type header is used to indicate the media type of the resource. The media type is a string sent along with the file indicating the format of the file. For example, for image file its media type will be like image/png or image/jpg, etc. In response, it tells about the type of returned content, to the client.


1 Answers

header('countryname: USA');
print_r(headers_list());

Array ( [0] => X-Powered-By: PHP/5.3.0 [1] => countryname: USA ) 

...works for me.

Are you sure you haven't output anything before it? You cannot set headers after you've started printing text. Use headers_sent() to see if the headers have already been sent (that is, if you've already output something).

like image 171
Andreas Avatar answered Oct 10 '22 13:10

Andreas