ajaxphp-7http-status-code-500

Error 500 in AJAX with PHP7 during redirection


I switched my site from php5.5 to php7, and I now have a very strange bug: When I do a particular ajax request which contain a redirection, the server answer with a http 500 error code, while returning the good html content (I can see it from the Chrome console).

When I do the exact same request in php5, I have no more error. When I do the exact same request in php7 in a new tab without ajax, I have no more error. And stranger, when I sometime add a var_dump in my code, I have no more error.

When I have the 500 error code, I have nothing coming in my logs, and no error is displayed in the html content.

Here is the response header with error:

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

And without error (after I add a 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

Do you have any idea of what could be wrong?


Solution

  • I found the problem, there was a custom exception somewhere in the code I used wich launched a header with a 500 http error by default, but the script was not killed after the header was launched by the exception, so it was very hard to find it :

      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;
      }
    

    But I still don't know why the problem was only in php7 and only in certain condition, and I have no time to find.