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?
“Go back” link via PHP, HTML, & JavaScript And here is the PHP code to print a “Go back” text link: echo '<p><a href="javascript:history.go(-1)" title="Return to previous page">« Go back</a></p>'; As before, customize the text and attributes as desired.
There are two approaches used to redirect the browser window back. Approach 1: Using history. back() Method: The back() method of the window. history object is used to go back to the previous page in the current session history.
Redirection from one page to another in PHP is commonly achieved using the following two ways: Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) header to the client.
Get Last URL Segment If you want to get last URI segment, use array_pop() function in PHP.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With