Try this:
$previous = "javascript:history.go(-1)";
if(isset($_SERVER['HTTP_REFERER'])) {
$previous = $_SERVER['HTTP_REFERER'];
}
in html:
<a href="<?= $previous ?>">Back</a>
The JavaScript code is initialize as fallback for HTTP_REFERER
variable sometimes not work.