美文网首页
php重定向三方法

php重定向三方法

作者: CastarWang | 来源:发表于2019-04-15 15:26 被阅读0次

    今天没事研究一下php的重定向,感觉有点用处,权当笔记了。

    方法一:使用php的header()

    场景1:从index.php重定向到index2.php

    创建两个文件,分别为index.php和index2.php,其文件内的代码如下:

    index.php:

    <?php

      header(“Location:index2.php”);

    ?>

    index2.php:

    <?php

    echo “这是index2页面”;

    ?>

    解析:

    index.php与index2.php同在web目录下,因此header()里直接写相对路径(index2.php)即可,另外说明一下header()的用法,如下:

         “header函数向客户端发送原始的 HTTP 报头,必须在任何实际的输出被发送之前调用 header() 函数”

    场景2:重定向到http://www.castarwang.com/index.php

    修改场景1中index.php的代码即可,修改如下:

    index.php:

    <?php

      header(“Location:http://www.castarwang.com/index.php”);

    ?>

    方法二:

    <?php

    echo “<script>window.location=\”index2.php\”</script>”;

    ?>

    效果:跳转到index2.php

    方法三:

    <?php

     echo “

    \<meta HTTP-EQUIV=\”Refresh\” CONTENT=\”2;URL=index2.php\”\>

    “;

     ?>

    效果:2秒后跳转到index2.php

    这里就不详细说明了, 记住怎么用就行了 : , )

    本文由CastarWang创作,转载和引用遵循知识共享署名-非商业性使用 2.5 中国进行许可。

    相关文章

      网友评论

          本文标题:php重定向三方法

          本文链接:https://www.haomeiwen.com/subject/uwiuwqtx.html