6

How do I redirect to a previous page using header("Location:...") ? The problem occurs when a user is scrolling down in a page to find a link for example, then clicks it - opens another page, clicks the link I've given "Go back to links (header("Location:links.php");)", but when the user clicks it, it will head to previous page but on the top part of the page.

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

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
RunCode
  • 61
  • 1
  • 1
  • 5

3 Answers3

17

try this

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

Note that this may not work with secure pages (HTTPS) and it's a pretty bad idea overall as the header can be hijacked.

or

header("location:javascript://history.go(-1)");
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ezhil
  • 996
  • 8
  • 13
2

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

'HTTP_REFERER'

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
-2

Looks like you should add some javascript to the page to perform scrolling. Here is an example of such implementation with Jquery: how to remember scroll position of page

Community
  • 1
  • 1