Its so simple just use this
header("location:javascript://history.go(-1)");
Its working fine for me
Just a little addition:
I believe it's a common and known thing to add exit;
after the header function in case we don't want the rest of the code to load or execute...
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
Just try this in Javascript:
$previous = "javascript:history.go(-1)";
Or you can try it in PHP:
if(isset($_SERVER['HTTP_REFERER'])) {
$previous = $_SERVER['HTTP_REFERER'];
}
You have to save that location somehow.
Say it's a POST form, just put the current location in a hidden field and then use it in the header()
Location.
Storing previous url in a session variable is bad, because the user might right click on multiple pages and then come back and save.
unless you save the previous url in the session variable to a hidden field in the form and after save header( "Location: save URL of calling page" );
Source: Stackoverflow.com