do I need to use exit after header(“Location: http://localhost/…”);?

后端 未结 6 1215
清酒与你
清酒与你 2021-02-19 02:53

I\'m creating a script to validate a form and I was asking myself a question. When I use a header (see example below), do I need to use exit right after? I mean, does using head

6条回答
  •  粉色の甜心
    2021-02-19 03:23

    Although the answers above sound great, if you're unsure of your code path, this could lead to unexpected results. For example, if you're using a framework that relies on the fact that code execution will run from beginning to end, you may inadvertently interrupt it.

    This might be okay from a user perspective as they will still be redirected and will be none the wiser, but consider the following:

    You're using a framework (OS or custom) that is expecting to log redirects, or set additional headers (or any number of other items). By calling exit, you're circumventing that logic and therefore may get unexpected results.

    So in short, yes the above methods will work, just a word of caution to know what you're expecting to happen before short circuiting it.

提交回复
热议问题