It is important to point out that its two solutions redirect the frontend. It is not because there is an excerpt of PHP there that it does in the backend; PHP will only be responsible for generating the HTML delivered by the HTTP response.
First rule of web development? You don’t talk about web development. Second rule of web development? You don’t talk about web development. Third rule of web development? Never trust incoming HTTP request data.
Your second option, you may not know, is entirely based on a data from the HTTP request that PHP is handling, referring to the HTTP header Referer
. The very documentation PHP, which you’ve probably read, warns you about it:
HTTP_REFERER
The address of the page (if any) through which the user’s agent accessed the current page. This policy is informed by the user’s agent. Not all browsers generate this Header, and some still have the ability to modify HTTP_REFER content as a resource. In a nutshell, unreliable.
Detached parts on my own. In short, there are no justifications for making your application dependent on something that may not be defined and when it is, it may not be reliable.
On the other hand, you have a stable history management API, managed by the browser itself. Your user is in a browser, your intention is that he goes back to the previous page, no one better to tell you what this page is than the browser itself.
Other than that, the API history
browser is compatible with the changes the application may want to make to the history itself. The application can, for example, make use of history.pushState
to set points in the history for states of the page itself and allow the user to want to navigate between such states with the browser tools. As an example, it may be desired that after opening a modal on the screen the Back button of the browser closes it causing the user to return to the previous state, which was the page with the modal closed. In such cases, your return link would be consistent with the behavior of the application itself. With the link set with PHP the user would be redirected independent of the page’s internal states.
Therefore, use window.history.back()
, it is supported by 97.04% of current browsers, as per Can I Use.
In front-end, because that’s when the data has already been managed in the database and received a response that everything is okay (or not).
– Edward Ramos
@Edwardramos How so? Both redirects of the question are in the frontend, the only difference is that the former uses the API
history
and the second defines the URL based on the data of the request received by PHP.– Woss