How can the PHP code replace the JavaScript function history.back() for creating a "Back Button"?
To replace the JavaScript function history.back() with PHP, we can use the header() function to redirect the user to the previous page. This can be achieved by using the $_SERVER['HTTP_REFERER'] variable, which contains the URL of the previous page visited by the user.
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
header('Location: /'); // Redirect to home page if no previous page is available
}
exit;
?>
Keywords
Related Questions
- How can getimagesize be used to determine the size of images extracted from URLs in PHP?
- What are the best practices for type checking and comparison in PHP to avoid potential issues with weak typing?
- How can the use of isset() and empty() functions help in avoiding errors with form submissions in PHP?