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_li开发者_运维技巧st
doesn't show it.
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).
in server file add
header("Access-Control-Allow-Headers: custom_header_name");
in request for example AJAX add add custom_header_name
and value
for example
$.ajax({ url:your_url_to_server, method:'POST', data:dat, headers:{ "token_name":"19980", }, dataType:'json', success:(data)=>{ console.log(data) } })
then at server side you can access the value of the header by using
$_SERVER['HTTP_CUSTOM_HEADER_NAME'];
i hope this also works for you...
精彩评论