Redirect to previous page: PHP vs. Javascript

Asked

Viewed 232 times

5

What is the most advantageous way to make the user return to the previous page. For example, in Frond-end would look like this:

<a href="window.history.back()">Voltar</a>

Already in the back-end would be something similar to that:

<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>">Voltar</a>
  • What is the most appropriate approach?
  • There are better approaches?
  • What safer alternatives exist to do this in the back-end?
  • 1

    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).

  • @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.

1 answer

2


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.

  • 1

    @rcs How to use sessions? I didn’t understand the relationship between the session and the redirect to the previous page.

  • It depends exclusively on the requirements of the system. Redirecting on the frontend or backend are very different things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.