前言
本文介绍的是在PHP中用header("location:test.php")进行跳转要注意以下几点,有助于解决一些新手经常遇到的问题
一、location和“:”号间不能有空格,否则会出错。
二、在用header前不能有任何的输出。
三、header后的PHP代码还会被执行。
前往查看:php header()函数 使用方法
1.php跳转代码格式:
<?php
$url = $_GET[‘url’];
Header(“Location:$url”);
?>
2.php中下例代码会报错:
<html><head></head><body>
<?php
header(“location:../test.php”);
?>
</body></html>
只能这样:
<?php
header(“location:../test.php”);
?>
<html><head></head><body>…</body></html>
即header函数之前不能向客户发送任何数据.
3.header后的PHP代码还会被执行及跳出执行
<?php
header(“location:../a.php”);
header(“location:../b.php”);
?>
<html><head></head><body></body></html>
我们发现它重定向b.php.
而php在执行header后,继续执行下面的代码.
在项目中要实现重定向后不执行后面的代码可以这样操作:
if(…)
{ header(“…”);exit();}
<?php
//重定向浏览器
header(“Location:http://www.demo.com”);
//确保重定向后,后续代码不会被执行
exit;
?>
总结
以上就是这篇文章的全部内容了,希望能对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。

