PHP Header用于页面跳转要注意的几个问题总结
author:一佰互联 2019-05-01   click:176

1.header()函数

header()函数是PHP中进行页面跳转的一种十分简单的方法。header()函数的主要功能是将HTTP协议标头(header)输出到浏览器。

header()函数的定义如下:

void header (string string [,bool replace [,int http_response_code]])
可选参数replace指明是替换前一条类似标头还是添加一条相(www.yinxi.net)同类型的标头,默认为替换。

第二个可选参数http_response_code强制将HTTP相应代码设为指定值。 header函数中Location类型的标头是一种特殊的header调用,常用来实现页面跳转。注意:

1.location和“:”号间不能有空格,否则不会跳转。
2.在用header前不能有任何的输出。
3.header后的PHP代码还会被执行。例如,将浏览器重定向到jb51.net

<?php 
 //重定向浏览器 
header("Location: //www.yinxi.net"); 
 //确保重定向后,后续代码不会被执行 
exit;
?>

1、php跳转代码一句话式:

<?php 
$url = $_GET["url"];
Header("Location:$url");
?>

2、php跳转代码if判断式:

复制代码 代码如下:
if($_COOKIE["u_type"]){ header("location:register.php"); } else{ setcookie("u_type","1","86400*360");//设置cookie长期有效 header("location:zc.html");

注:保存为zc.php,当用户访问zc.php时,判断一个cookie是否存在,如果存(www.yinxi.net)在就跳转到register.php,如果不存在则创建cookie然后跳转到zc.htmlfrom://www.yinxi.net/phper/php-cy/62883.htm

URL重定向函数

// URL重定向
function redirect($url, $time=0, $msg=”) {
 //多行URL地址支持
$url = str_replace(array(“n”, “r”), ”, $url);
 if ( empty($msg) )
 $msg = “系统将在{$time}秒之后自动跳转到{$url}!”;
if (!headers_sent()) {
 // redirect
 if (0 === $time) {
 header(‘Location: ‘ . $url);
 } else {
 header(“refresh:{$time};url={$url}”);
echo($msg);
 }
 exit();
 } else {
 $str = “<meta http-equiv="Refresh" content="{$time};URL={$url}">”;
if ($time != 0)
 $str .= $msg;
 exit($str);
 }
 }

上面的不能返回404状态,如果是页面跳转之后返回404状态代码我们可如下操作

function getref()
 {
 $url = @$_SERVER["HTTP_REFERER"];
 if( !empty( $url ) )
 {
 if( !strstr($url ,"jb51.net" ) && !strstr($url,"jb51.net"))
 {
 @header("http/1.1 404 not found");
 @header("status: 404 not found");
 include("404.html");//跳转到某一个页面,推荐使用这种方法
 exit();
 }
 }
 else
 {
 @header("http/1.1 404 not found");
 @header("status: 404 not found");
 include("404.html");//跳转到某一个页面,推荐使用这种方法
 exit();
 }
 }

如果要做301也差不多

<?php 
 $the_host = $_SERVER["HTTP_HOST"];
 $request_uri = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : "";
 if($the_host !== "www.yinxi.net")
 {
  //echo $_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: //www.yinxi.net" . $_SERVER["PHP_SELF"] . $request_uri);
 }
 ?>

下面是和asp中重定向response.redirect的比较:
例1:
response.redirect "../test.asp"
header("location:../test.php");
两者区别:
asp的redirect函数可以在向客户发送头文件后起作用.

<html><head></head><body>
<%response.redirect "../test.asp"%>
</body></html>
查是php中下例代码会报错:
<html><head></head><body>
<?
header("location:../test.php");
?>
</body></html>
只能这样:
<?
header("location:../test.php");
?>
<html><head></head><body>...</body></html>
即header函数之前不能向客户发送任何数据.
例2:
asp中
<html><head></head><body>
<%
response.redirect "../a.asp"
response.redirect "../b.asp"
%>
</body></html>
结果是重定向a.asp文件.
php呢?
<?
header("location:../a.php");
header("location:../b.php");
?>
<html><head></head><body></body></html>
我们发现它重定向b.php.
原来在asp中执行redirect后不会再执行后面的代码.
而php在执行header后,继续执行下面的代码.
在这方面上php中的header重定向不如asp中的重定向.有时我们要重定向后,不能执行后面的代码:
一般地我们用
if(...)
header("...");
else
{
...
}
但是我们可以简单的用下面的方法:
if(...)
{ header("...");exit();}
还要注意的是,如果是用Unicode(UTF-8)编码时也会出现问题,需要调整缓存设置.
<[email=%@]%@LANGUAGE="VBSCRIPT[/email]" CODEPAGE="936"%>
<%if Request.ServerVariables("SERVER_NAME")="s.jb51.net" then
response.redirect "news/index.htm"
else%>
<%end if%>
<script>
var url = location.href;
if(url.indexOf("//www.yinxi.net/")!=-1)location.href="/index/index.htm";
if(url.indexOf("http://www.kanshule.com/")!=-1)location.href="/index1/index.htm";
if(url.indexOf("http://www.shouji17.com/")!=-1)location.href="/cn/index.asp";
if(url.indexOf("http://www.baidu.com/")!=-1)location.href="/cn/index.asp";
</script>