How can PHP achieve dynamic page redirection?
To achieve dynamic page redirection, you can utilize the header function in PHP. This function is used to send raw HTTP headers, including instructions to redirect to another page.
Here is a basic example demonstrating how to use the header function for page redirection:
<?php
// 跳转到指定页面
header("Location: http://www.example.com/new_page.php");
exit();
?>
In the example above, when a user accesses this PHP page, they will be immediately redirected to the http://www.example.com/new_page.php page.
Please note that the header function must be called before any content is output on the page, otherwise it may result in an error. It is best to place the header function at the very beginning of the PHP code.
Additionally, you can achieve page redirection using JavaScript. For instance, by using the following code:
<?php
echo '<script>window.location.href="http://www.example.com/new_page.php";</script>';
?>
By doing this, you can also achieve page redirection, but it is recommended to use the header function for a more standardized and efficient page redirection.