Redirect back to previous page in PHP

Redirect back to previous page in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Simple Redirects Lose Scroll Position and How to Fix It

When a user clicks a link that opens a new page, performs an action, and then follows a header("Location: links.php"); redirect, they invariably expect to remain where they left off. The browser's native "Back" button handles this state perfectly: it restores the entire session, including the viewport position. However, PHP redirects are server-side jumps that reset the client's context entirely. When you issue a standard 302 or 301 redirect, the browser treats it as a fresh navigation event to links.php, loading the document from the top (scroll-y: 0) by default. This behavior is inherent to how HTTP works, not a bug in your PHP code.

To replicate the "Back" button experience using a server-side redirect, you must explicitly tell the client where to scroll once the new page loads. Since header("Location:...") terminates the script immediately after sending headers, you cannot use window.scrollIntoView() directly within that block. Instead, the solution lies in leveraging the URL fragment identifier (the part of the URL after the #).

If you know the specific ID of the element the user was viewing when they clicked the link, you can append it to the redirect target. For instance, if the "links" page contains a section with <div id="my-links">, your PHP code should look like this:

<?php
// Assuming you know the ID of the section the user was on
$targetSection = 'my-links'; 

header("Location: links.php#" . $targetSection);
exit(); 
?>

When the browser receives this request, it parses the URL and automatically scrolls to the element matching #my-links. This is the standard mechanism for single-page applications (SPAs) and many modern landing pages. However, this approach has a critical limitation: you must know the ID of the destination section beforehand. If the user clicked a link from the middle of a generic list without an associated ID, or if the target page doesn't have named sections, this method fails to provide the seamless experience the user expects.

There is a more robust pattern for handling "Go back" scenarios where you don't know the exact scroll position but want to avoid jumping to the top. The most reliable way to achieve true state restoration is to pass the scroll position or hash context via query parameters, provided your target page supports it. You can append a ?scroll=auto parameter if your frontend framework (like Vue or React) handles that logic, or simply rely on the fragment approach if you are dealing with named anchors.

Consider the trade-off here. A simple redirect to links.php is clean and semantic but resets the view. Adding a fragment (#section-id) requires cooperation from the target page's HTML structure. If your target page is dynamic or lacks specific IDs, you might need to pass state in the session or URL query string (e.g., ?came_from=scroll_50_percent) and handle the scrolling on load within the target script.

It is also worth noting that JavaScript-based redirects are often confused with PHP redirects. You cannot use window.location.href inside a PHP block to scroll; that code executes in the browser, not the server. If you are using a framework like Laravel, the logic remains the same regarding headers, but the syntax changes to redirect()->to('links.php#section').

Ultimately, achieving the "Back" button feel requires acknowledging that a redirect is a state reset. By appending a fragment identifier to your destination URL, you can manually restore the viewport position without requiring the user to scroll back up and down again.

Our network