如何在 PHP 中设置自定义标头
JAVA 开发人员在标头中向我发送数据。我这样想
$_SESSION["HTTP_COUNTRYNAME"];
如何用标头做出响应?它尝试了 header("countryname: USA");
但 php function headers_list
没有显示它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
...对我有用。
你确定之前没有输出过任何东西吗?开始打印文本后无法设置标题。使用
headers_sent()
查看标头是否已发送(即,是否已输出某些内容)。...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).在服务器文件中添加
header("访问控制允许标头:custom_header_name");
在请求中,例如 AJAX 添加
custom_header_name
和value
例如
$.ajax({ url:your_url_to_server, method:'POST', data:dat, headers:{ "token_name":"19980", }, dataType:'json', success:(data)=> ;{ console.log(data) } })
然后在服务器端您可以使用以下方式访问标头的值
$_SERVER['HTTP_CUSTOM_HEADER_NAME'];
我希望这也适合你......
in server file add
header("Access-Control-Allow-Headers: custom_header_name");
in request for example AJAX add add
custom_header_name
andvalue
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...