I have been looking for a reliable back button, here is the solution I like better than history.go(-1)
or $_SERVER["HTTP_REFERER"]
or others that use header and session tricks like these:
header('Cache-Control: no cache');
, session_cache_limiter('private_no_expire');
which I found equally unreliable for my application.
Background: I have a search form that gets you to a result set page. Results page has typical CRUD-type functionality to view or edit a record using both GET and POST. Result set page also has links to other subpages to add/edit various record details. Users need to be able to navigate to the result page and the subpages and still be able to get back to the original result set.
Solution: The search form sends the user to an intermediate page which sets some session variables that retain the original search parameters and then directs the user to the page that displays the result set.
The search page is a basic search form which includes a search field and search value.
intermediate page:
session_start();
// Unset the variable if it exists
unset($_SESSION['search']);
unset($_SESSION['field']);
unset($_SESSION['value']);
if (isset($_POST['search'])){
$_SESSION['search'] = $_POST['search'];
$_SESSION['field'] = $_POST['field'];
$_SESSION['value'] = $_POST['value'];
}
// All sessions are set so redirect user to the results
header('Location: https://www.yourwebsite.com/results.php');
back button code used:
<a href='results.php'> < Back </a>
I'd be happy to hear if anyone can improve upon this solution. I'll try and set up a working example on fiddle or someplace if there is interest.