Redirect back to previous page in PHP

How to redirect to the previous page using header("Location:...") ? The problem occurs when the user scrolls on the page to find a link, for example, then clicks it - opens another page, clicks the link that I gave "Return to links ( header("Location:links.php"); ), but when the user clicks on it, it will be directed to the previous page, but to the top of the page.

The user should scroll down the page where he found the link that he just clicked (which is frustrating). Is there a php code like the 'back'-button used in web browsers where you return to the exact location and page right before you click anything else?

+6
source share
3 answers

try it

 header('Location: ' . $_SERVER['HTTP_REFERER']); 

Note that this may not work with secure pages (HTTPS), and this is a pretty bad idea, as the header may be captured.

or

 header("location:javascript://history.go(-1)"); 
+11
source

Try the following: header('Location: ' . $_SERVER['HTTP_REFERER']);

'HTTP_REFERER'

The address of the page (if any) that linked to the user agent on the current page. This is set by the user agent. Not all user agents will install this, but some provide the ability to modify HTTP_REFERER as a feature. In short, one cannot really trust him.

+1
source

It looks like you should add javascript to the page to scroll. Here is an example of such an implementation with jQuery: how to remember page scroll position

-1
source

Source: https://habr.com/ru/post/969990/


All Articles