Valid HTTP 1.1 header response code after form submission

In my MVC structure, I sometimes redirect after submitting the form. Let's say you submit the form to / example / input.

I want to add the correct header code and explanatory text in PHP, for example. header('HTTP/1.1 404 Not Found');

1) Your entry contains errors. You stay on the page / example / input and again get the form marked with errors, etc. Which is HTTP 1.1. Would the code and text be correct to send with this redirect command?

2) Your input is OK, the item is saved, and you are redirected via Header('Location: ...') to / example / success. Which is HTTP 1.1. Will the code and text be correct here?

3). The PHP code throws an error due to incorrect configuration, missing include file, damaged database connection, or something else is wrong. Which is HTTP 1.1. Will the code and text be correct here?

I looked at the codes here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html Number 200 is displayed on the right for 1), and number 301/302 is displayed on the right for 2), and 500 is for 3) . But in all three cases, I believe that the heading / explanation, following the codes on the link above, does not quite correspond to the scenario that I describe above. Should I choose other codes / text?

+4
source share
2 answers

Cases 1 and 2 describe variants of the same situation: you submit the POST form, the server processes it and redirects the client to the success page or back to the form. In both cases, "303 See Other" is the correct answer. This is the correct way to redirect the client to the resource using the GET method after the server has correctly processed the POST request. According to specification:

This method exists primarily to provide the output of a POST-activated script to redirect the user agent to the selected resource.

For case 3, code 500 is usually suitable for most critical errors.

+3
source

In my understanding, code 200 is correct if your PHP is successfully executed. So take care of 1 and 2.

For 3, PHP is already sending 500 code if a fatal error occurs.

To explain a little more, the 300s, when the resource is no longer at the requested URL. Therefore, you must redirect them to a new or right place. In your case, there is a resource, so you do not need the 300 code.

+1
source

All Articles