简体   繁体   中英

Redirect back to previous page in PHP

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?

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)");

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM