How can PHP be used to retrieve the HTTP_REFERER value for a back button functionality?
To retrieve the HTTP_REFERER value for a back button functionality in PHP, you can use the $_SERVER['HTTP_REFERER'] variable. This variable contains the URL of the referring page from which the current page was accessed. By storing this value in a session variable, you can use it to redirect the user back to the previous page when needed.
session_start();
if(isset($_SERVER['HTTP_REFERER'])) {
$_SESSION['previous_page'] = $_SERVER['HTTP_REFERER'];
}
// To redirect back to the previous page:
if(isset($_SESSION['previous_page'])) {
header('Location: ' . $_SESSION['previous_page']);
exit;
} else {
// Default redirect if no previous page is set
header('Location: index.php');
exit;
}