PHP is partially successful returns 404 error.



  • This is from that project earlier. The guy that set up my job moved everything to a new server and changed everything over to https.

    Now the form I worked on doesn't work entirely.

    The form is from a php file. It's performing an ajax call to another php endpoint. The endpoint is supposed to send 3 emails, but only sends 1. It's hard to figure out what's going on because a 404 error comes back from the ajax post. So, why am I getting a 404, yet the php file is obviously loading and performing at least part of its function.



  • @xaade XRF issues? I've had those happen before if the script doesn't change the header right.



  • The PHP script could be settings its response code to 404 when it runs.



  • @anotherusername said in PHP is partially successful returns 404 error.:

    The PHP script could be settings its response code to 404 when it runs.

    Sounds like a case for Occam's Razor



  • Alternately, you could be calling a custom 404 handler without realizing it. Does the PHP script that you're calling definitely exist? As in, a PHP script by that name... it's unlikely, but possible, that the script you're calling doesn't exist by that name, but that a 404 error page has been hacked to handle the form submission to it. And if the 404 error page doesn't explicitly set the HTTP status code to something, it defaults to 404.

    In that case, it's quite possible that it always returned 404, and you just never noticed until it didn't send all of the emails it was supposed to and you began debugging. Or... it does set the response code, but it's producing an error first, so it never gets that far. Or, it is trying to set the response code, but it's already produced some output (like an error message) and it's unable to set the response header because the response headers have already been sent.



  • Isn't PHP lovely?



  • @anotherusername said in PHP is partially successful returns 404 error.:

    Alternately, you could be calling a custom 404 handler without realizing it. Does the PHP script that you're calling definitely exist? As in, a PHP script by that name... it's unlikely, but possible, that the script you're calling doesn't exist by that name, but that a 404 error page has been hacked to handle the form submission to it. And if the 404 error page doesn't explicitly set the HTTP status code to something, it defaults to 404.

    In that case, it's quite possible that it always returned 404, and you just never noticed until it didn't send all of the emails it was supposed to and you began debugging. Or... it does set the response code, but it's producing an error first, so it never gets that far. Or, it is trying to set the response code, but it's already produced some output (like an error message) and it's unable to set the response header because the response headers have already been sent.

    No, this is definitely new. I tested it before. I know for sure because if it succeeds it gives me log output (I send back json, and the json had an array of strings which were log statements) of what happened, and I was getting that before.



  • I assume you've done the obvious stuff, like check the server's error log? You can always debug the old-fashioned way of putting a bunch of print/log statements in the code and seeing which one is the last one to be printed.

    Also check the error_reporting and, if it's on a testing server, display_errors settings to be sure you're getting all of the errors.


  • Java Dev

    @anotherusername said in PHP is partially successful returns 404 error.:

    The PHP script could be settings its response code to 404 when it runs.

    I've done that. We rewrote all URLs to the same script in order to support pretty URLs. If then the URL didn't map to a valid page, we'd render a pretty error with full menu interface and flag it as 404.

    Unless the server was IIS. Because that would prefer its own 404 page over our pretty content.



  • Ok, so apparently in the php file I had a line that changed the header to application/json, and that didn't break it on the old server, but broke it on this one.

    :facepalm:

    Thanks for all the help.



  • @xaade said in PHP is partially successful returns 404 error.:

    Ok, so apparently in the php file I had a line that changed the header to application/json, and that didn't break it on the old server, but broke it on this one.

    :facepalm:

    Thanks for all the help.

    Difference in settings for output buffering? A lot of people abuse output buffering because they can't be bothered to determine all the headers before sending output.



  • @dragnslcr said in PHP is partially successful returns 404 error.:

    @xaade said in PHP is partially successful returns 404 error.:

    Ok, so apparently in the php file I had a line that changed the header to application/json, and that didn't break it on the old server, but broke it on this one.

    :facepalm:

    Thanks for all the help.

    Difference in settings for output buffering? A lot of people abuse output buffering because they can't be bothered to determine all the headers before sending output.

    🤷🏻

    I'm not that knowledgeable, and the dude that hired me knows it. He just wanted it to work.



  • @xaade Output buffering basically lets you work around this:

    @anotherusername said in PHP is partially successful returns 404 error.:

    it's already produced some output (like an error message) and it's unable to set the response header because the response headers have already been sent.

    When output buffering is turned on, it buffers a certain amount of output before it starts actually sending -- so if you need to send a header, it hasn't really sent anything yet because it's all buffered.

    But it would really only make a difference if the script was producing output before it sent headers (yours wasn't, from what I saw).


Log in to reply