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
- What are the potential pitfalls of using reserved names like 'time' or 'date' as column names in PHP MySQL queries?
- What are the advantages and disadvantages of using temporary files in PHP for handling simultaneous access to CSV files during data archiving processes?
- How can timestamps be used effectively in PHP for date comparisons?