How can PHP be used to dynamically redirect users to different pages based on their input URL?
To dynamically redirect users to different pages based on their input URL in PHP, you can use the header() function to send a raw HTTP header to perform the redirection. You can check the input URL using $_GET or $_POST superglobals and then use conditional statements to determine the destination URL for redirection.
<?php
$input_url = $_GET['url'];
if ($input_url == 'page1') {
header('Location: page1.php');
exit;
} elseif ($input_url == 'page2') {
header('Location: page2.php');
exit;
} else {
header('Location: error.php');
exit;
}
?>
Related Questions
- In the context of PHP, what are some recommended resources for learning and mastering regex patterns?
- How can the issue of concatenating instead of dividing be avoided in PHP calculations, as shown in the provided PHP script?
- How can beginners in PHP ensure proper error handling and validation when using includes for dynamic content loading?