Home  >  Article  >  Backend Development  >  Three ways to implement redirection in php

Three ways to implement redirection in php

伊谢尔伦
伊谢尔伦Original
2016-11-26 14:04:211123browse

1. Use PHP’s header function

That is, use PHP’s header function. The function of the header function in PHP is to issue control instructions to the browser that should be passed through the WEB server specified by the HTTP protocol, such as declaring the type of return information ("Context-type: xxx/xxx"), the attributes of the page ("No cache", "Expire"), etc.

The method of using HTTP header information to redirect PHP to another page is as follows:

<?php 
    $url = "www.yaojinbu.com";  
    if (!empty($url))    
    {    
        header("HTTP/1.1 303 See Other"); //这条语句可以不写  
        header("Location: $url");  
    }    
?>

Note: There is a space after "Localtion:".

2. Use HTML meta tag

Use HTML tag, that is, use META REFRESH tag, for example:

<?php 
    if (!empty($url))  
    {  
        echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=$url\">";  
    }  
?>

3. Use JavaScript script function

For example:

<?php 
    if (isset($url))  
    {  
        echo "<SCRIPT LANGUAGE="JavaScript">";  
        echo "location.href=&#39;$url&#39;";  
        echo "</SCRIPT>";  
    }  
?>

or

<?php echo "<script>window.location =\"$url\";</script>";?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn