Several Methods to Implement Page Redirection in PHP
Verified personally, not copied and pasted.
There are several ways to implement page redirection in PHP. After reading several articles that were not well-organized, I have compiled this summary.
Implementing in PHP Script Code
<?php header("location:url_address"); ?>
For example:
<?php header("location:helloworld.php"); ?>
The page will redirect immediately because the header function executes a location redirect.
Delayed Redirection (e.g., waiting a few seconds after a successful login before redirecting to another page)
<?php header("Refresh:seconds;url=url_address"); ?>
For example:
<?php header("Refresh:3;url=helloworld.php"); ?>
This will execute the redirect after 3 seconds.
Alternatively:
<?php sleep(3); header("location:url_address"); ?>
Calling the sleep() method achieves the same effect of a delayed redirect.
Implementing in JavaScript Script Code
1. window.location.href method
<script type="text/javascript">
window.location.href = "helloworld.php";
</script>
Using JavaScript to implement delayed redirection
<script type="text/javascript">
setTimeout("window.location.href='helloworld.php'", 3000);
</script>
2. window.location.assign method (Delayed redirection method is the same as above)
<script type="text/javascript">
window.location.assign("helloworld.php");
</script>
3. window.location.replace method (Replaces the current page with the new page, so it is not saved in the browser history, meaning you cannot use the browser's back button to return to the original page)
<script type="text/javascript">
window.location.replace("helloworld.php");
</script>
4. window.open method (Takes three parameters: the first is the URL address, the second is the target for opening the new page (e.g., _blank for a new window, _self for the current page), and the third specifies the features of the new page, such as dimensions and position).
<script type="text/javascript">
window.open("index.php", "_blank", "width=300px");
</script>
Implementing Redirection Using HTML Script Code
Execute code within the <head> tag.
Simply insert this line of code:
<meta http-equiv="refresh" content="3;url='helloworld.php'">
Original source: Several Methods to Implement Page Redirection in PHP. All rights reserved by the original author. If there is any infringement, please contact QQ: 337003006 for deletion.