ajax 和 php7 中的错误 500

Error 500 in ajax with php7

我将我的站点从 php5.5 切换到 php7,现在我有一个非常奇怪的错误:当我执行包含重定向的特定 ajax 请求时,服务器以 http 500 错误代码响应,同时返回良好的 html 内容(我可以从 chrome 控制台看到它)。

当我在 php5 中执行完全相同的请求时,我没有更多的错误。当我在没有 ajax 的新选项卡中在 php7 中执行完全相同的请求时,我没有更多错误。陌生人,当我有时在我的代码中添加一个 var_dump 时,我没有更多的错误。

当我有 500 错误代码时,我的日志中没有任何内容,html 内容中也没有显示错误。

这是响应 header 错误:

HTTP/1.1 500 Internal Server Error
Date: Mon, 03 Apr 2017 10:44:20 GMT
Server: Apache
Expires: Tue, 04 Apr 2017 10:44:20 GMT
Accept-Ranges: bytes
Set-Cookie: PHPSESSID=25e73849544a66f7512533246cde4d21; path=/
Last-Modified: Mon, 03 Apr 2017 10:44:20 GMT
Content-Length: 11718
Connection: close
Content-Type: text/html; charset=utf-8

并且没有错误(在我添加 var_dump 之后):

HTTP/1.1 200 OK
Date: Mon, 03 Apr 2017 10:45:44 GMT
Server: Apache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 3223
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

你知道哪里出了问题吗?

我发现了问题,在我使用的代码中某处有一个自定义异常,默认情况下启动了一个 header 并出现 500 http 错误,但是在 header 之后脚本没有被终止被异常启动,所以很难找到它:

  public function __construct($message = "Critical error", $code = 500, Exception $previous = Null) {
    parent::__construct($message, $code, $previous);
    if($this->getHttpHeader())
      header($this->getHttpHeader());
  }

  public function getHttpHeader() {
   $header = get_header_for_code($this->getCode());
   $this->http_header = $header;
   return $this->http_header;
  }

但是我还是不知道为什么问题只出现在php7,而且只出现在某些情况下,没时间去找。