【问题标题】:Response header duplicated (Lumen)响应标头重复(流明)
【发布时间】:2015-05-23 08:17:37
【问题描述】:

续:Setting response headers with middleware in Lumen

在 Lumen 中使用以下异常处理程序时,X-Powered-By 标头重复,即使 $replaceheader() 方法的第三个参数)默认为 true(即使手动设置它,如下所示,也不会工作)。

public function render($request, Exception $e)
{
    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
    {
        return response(view('not-found'), 404)->header('X-Powered-By', env('APP_NAME')."/".env('APP_VER'), true);
    }

    return parent::render($e);
}

响应头:

HTTP/1.0 404 Not Found
Date: Sat, 23 May 2015 08:05:13 GMT
Server: Apache
X-Powered-By: PHP/5.6.3
Cache-Control: no-cache
X-Powered-By: AppName/1.0.0
Connection: close
Content-Type: text/html; charset=UTF-8

唯一有效的方法是在调用->header 之前使用header_remove('X-Powered-By')。我不应该这样做,因为 $replace 参数已相应设置。

有没有更好的方法来防止X-Powered-By 标头重复?

【问题讨论】:

    标签: php http-headers lumen


    【解决方案1】:

    但是,如果我这样做,我无法让它与方法链一起使用:

    header('X-Powered-By: '.env('APP_NAME')."/".env('APP_VER'));
    return response(view('not-found'), 404);
    

    ...它按您的意愿工作。但请注意,根据 PHP 手册,标题中只有一个参数:

    void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
    

    可选的replace参数指示是否应该将标头 替换以前的类似标题,或添加第二个相同的标题 类型。默认情况下它将替换,但如果您将 FALSE 作为第二个传递 参数,您可以强制多个相同类型的标头。

    来源:http://php.net/manual/en/function.header.php

    ...意思是,它不是像 str_replace 这样的“用那个替换这个”类型的项目。如果您在第一个参数中键入的字符串与另一个标题项相似,它将用您自动键入的任何内容替换它。

    旁注:我还尝试将响应函数的第三个参数设置为包含 X-Powered-By 标头的数组,但无济于事。

    【讨论】:

      【解决方案2】:

      设置

      暴露_php =关闭

      在你的 php.ini 中删除

      X-Powered-By: PHP/5.6.3

      expose_php

      【讨论】:

      • 我知道我可以做到,但是为什么原始标题没有被替换?它肯定是在我的header() 电话之前发送的吗?如果我用.htaccess 来做,它可以工作。
      猜你喜欢
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2013-02-14
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 2017-06-04
      相关资源
      最近更新 更多