PHP redirect – go back to previous page

By | September 1, 2023

To go back to the previous page the superglobal variable $_SERVER can be used.
$_SERVER['HTTP_REFERER'] has the link to the previous page.

So to redirect simply :

// Method to go to previous page
function goback()
{
	header("Location: {$_SERVER['HTTP_REFERER']}");
	exit;
}

goback();
About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

14 Comments

PHP redirect – go back to previous page
  1. Tony

    The title should be “go forward to previous page” as if you cycle through with the back button, it redisplays all the intermediate pages.

Leave a Reply

Your email address will not be published. Required fields are marked *