How to detect if a Browser Back button was used to navigate to the current page via PHP

612 views Asked by At

Is there some indicator available in PHP to find out, if the user arrived at the current page by pressing the Back-Button of his browser?

This is the current setup of the page: The user currently navigates the website via a search or category page that uses ajax based filters. When clicking a filter, the result part of the search page is updated and the active filters are stored in the session.

Everytime the user starts a new search or a new category page a GET parameter is sent with the url, that makes the website remove the filters.

The problem is now, that when the user applied some filters, then clicks on a result and then uses the back button, the page treats this, as if a new search or category was started (removes the filters), since the GET Parameter is still in place.

My idea is to somehow detect, that the user navigated to this current page by using the back button and to disable the reset of the session in this particular case, so the user sees his filtered result list again. If this was possible somehow within the PHP code, it would safe me plenty of time.

2

There are 2 answers

1
Trần Quang Sơn On

You can try this to see how the navigation to your page was done.

PerformanceNavigation.type [DEPRECATED but link for you to see an overview]

Up to date for navigation type

0
Levi Pike On

I ended up not using PHP for this. Instead I used Javascripts history.replaceState() to replace the current url with one without this particular GET Parameter that would remove the filters. So in case of a back button navigation, the url is loaded without it and the filters are still applied.