Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 233
RunCode Avatar asked May 28 '14 16:05

RunCode


People also ask

How do I go back to the previous page in PHP?

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

How do I redirect to the last page?

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.

Can PHP redirect to another page?

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.

How can I get the last part of a URL in PHP?

Get Last URL Segment If you want to get last URI segment, use array_pop() function in PHP.


2 Answers

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)");
like image 72
Ezhil Avatar answered Sep 28 '22 08:09

Ezhil


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.

like image 28
Ali Gajani Avatar answered Sep 28 '22 08:09

Ali Gajani