4
Auhrii
289d

Why can no-one, not one single solitary fucker, on StackOverflow get it through their thick skull that when I call PHP's http_response_code() or try to get $_SERVER["REDIRECT_STATUS"], I want the response code from Nginx? No, not Apache. No, I don't want to pass a status code FROM PHP TO NGINX, I want the response code. FROM Nginx. TO PHP.

In what fucking universe does PHP know more about the response code than Nginx? It doesn't. Nginx knows the response code, because that's the fucker that redirected to the error page. I want the error. Passed to the page. From Nginx. To PHP.

NO, http_response_code() DOES NOT MAGICALLY FUCKING WORK, IT RETURNS 200 BY DEFAUL- fuck it.

Comments
  • 1
    PHP confuses the shit out of me as well.
  • 0
    @donkulator I finally figured it out in the end, at least! I had to jank my way around http_response_code() entirely and set the REDIRECT_STATUS parameter in fastcgi.conf to $status

    This let me get the response code from Nginx using $_SERVER["REDIRECT_STATUS"] in PHP, with the caveat that 200 (OK) returns as 000

    It's so janky, but it works
  • 1
    As someone who understands what you did in your last comment ...

    ... And partially what you try to achieve...

    What the fuck is your admin doing?
  • 1
    @IntrusionCM I regret to inform you that I am my own admin, and I have no idea what the fuck I'm doing
  • 0
    @Auhrii

    https://php.net/manual/en/...

    What you're doing is simply put a violation and abusal of an old / ancient variable.

    If I understand you correctly, you have

    NGINX in front of PHP
    Client calls Website
    Client is a special kind of retard
    NGINX redirects to an error page
    NGINX calls the PHP script

    Now you want to know what "Client is a special kind of retard" call created as a status code?

    As in "redirect to the error page AND add via header the previous status code" as an example to solve it?
  • 0
    @IntrusionCM Yeah, pretty much. I'd have assumed that http_response_code() would give me, y'know, the *actual response code* and not just 200 by default. Nginx knows what the response code is and it can evidently already pass additional information to PHP, but in this case it doesn't.

    The error page redirect always worked fine, no problems there, it's just that PHP is a dumbass and is configured to assume "oh, I'm running? must be 200 OK" by default.
  • 1
    @Auhrii

    http://nginx.org/r/...

    http://nginx.org/en/docs/...

    You're thinking wrong, though it depends largely on how NGINX is configured.

    What usually happens with PHP FPM CGI is the following:

    Website - NGINX - resolve to location
    ...
    parse fastcgi parameters
    call CGI binary with fastcgi parameters

    There is no HTTP code involved per se.

    What you did with your modification is to include the $status variable in the fastcgi parameters, which should be the last http status code.

    000 as there is no status code when there is no error - the binary just gets called, there is no http request happening.

    https://gist.github.com/kerns/...

    Here is one gist I found which is an example of how to create an error page with query parameters to pass the status code.

    Less hacky.

    Alternative would be an error page with a proxy pass and setting a header.
Add Comment