PHP中的标头会覆盖HTTP响应代码 [英] Header in PHP overrides the HTTP Response code

查看:184
本文介绍了PHP中的标头会覆盖HTTP响应代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Apache级别处理设置规则,发现php中的标题('Location:filename.php')会覆盖响应代码。请看下面的代码: -

Working around setting rules at Apache level, Found that header('Location: filename.php') in php overrides the response code. Look at the below code :-

<?php
header('HTTP/1.1 308 Permanent Redirect'); //or Try for 4xx/5xx Code
header('Location: http://hello.php');
?>

预期结果


  • HTTP / 1.1 308永久重定向

  • 主机:localhost:8000

  • 位置:hello.php

  • 连接:关闭

  • X-Powered-By:PHP / 5.5.6

  • HTTP/1.1 308 Permanent Redirect
  • Host: localhost:8000
  • Location: hello.php
  • Connection: close
  • X-Powered-By: PHP/5.5.6

实际结果


  • HTTP / 1.1 302找到

  • 主机:localhost:8000

  • 位置:hello.php

  • 连接:关闭

  • X-Powered-By:PHP / 5.5.6

  • HTTP/1.1 302 Found
  • Host: localhost:8000
  • Location: hello.php
  • Connection: close
  • X-Powered-By: PHP/5.5.6

Apache级别的错误或PHP中的错误?

Is something wrong at Apache level or its Bug in PHP ?

更新:

推荐答案

根据函数文档页面这是PHP发送Location头时所需的行为。

As per the function documentation page this is the desired behaviour of PHP when sending the Location header.

编辑:对不起,误读了你的消息。如果您发送的代码不同于3xx,它将覆盖响应代码,因此它应该使用308.
您确定没有其他HTTP代码被发回吗?您的客户是否尝试检索新资源并且您将302视为最终结果?

Sorry, misread your message. It will override the response code if you send a code different than 3xx, so it should work with a 308. Are you sure that no other HTTP code is sent back ? Does your client tries to retrieve the new resource and you see the 302 as a final result ?

最终编辑:您是否尝试过

FINAL Did you try

header('Location: /some/page.php', TRUE, 308);

这篇关于PHP中的标头会覆盖HTTP响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆