This article mainly introduces to you the detailed explanation of the three methods of PHP to implement page redirection.
So what is page redirection?
In the process of website development, when we encounter web page migration or website adjustment, we need to make a redirection to prevent traffic loss. Page redirection is to redirect various network requests to other locations through various methods.
Below we will introduce to you the specific method of implementing page redirection through simple code examples.
Method 1: header redirection
<?php $url = "http://php.cn"; if (isset($url)) { header("Location:$url"); } else { echo "没有跳转的地址!"; }
Here we define a $url variable, which represents the url address to be redirected to. Then use if to determine whether the redirect link exists. If it exists, jump to the new address "http://php.cn". If not, output "No jump address!".
If no jump link is defined, the return value is as follows:
If a new url is defined, it is normal The jump is as follows:
Note : The header() function sends the original HTTP header to the client. The parameter is the new url address.
Method 2: js script redirection
<?php $url = "http://php.cn"; if (isset($url)) { echo "<SCRIPT language= 'JavaScript'>location.href='$url'</SCRIPT>"; } else { echo "没有跳转的地址!"; }
Also first determine whether the link exists, and then here we mainly use location.href in js, that is It means jump.
Method three: html tag redirection
<?php $url = "http://php.cn"; if (!isset($url)) { exit("没有跳转的地址!"); } ?> <HTML> <head> <meta HTTP-EQUIV="REFRESH" CONTENT="3; URL='<?php echo $url; ?>' "> </head> <body> </body>
Similarly, we first determine whether there is a jump link, and then mainly use
In the tag, REFRESH means defining a refresh, 3 is the refresh time, the unit is seconds, and the parameter in the URL is the refreshed file, that is, the new jump link address.
This article is a detailed introduction to the three methods of implementing page redirection in PHP. I hope it will be helpful to friends in need!
If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial. Everyone is welcome to learn and refer to it.
The above is the detailed content of How to implement page redirection in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!