程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP Header用於頁面跳轉要注意的幾個問題總結

PHP Header用於頁面跳轉要注意的幾個問題總結

編輯:PHP綜合

1.header()函數

header()函數是PHP中進行頁面跳轉的一種十分簡單的方法。header()函數的主要功能是將HTTP協議標頭(header)輸出到浏覽器。

header()函數的定義如下:

void header (string string [,bool replace [,int http_response_code]])
可選參數replace指明是替換前一條類似標頭還是添加一條相(www.jb51.net)同類型的標頭,默認為替換。

第二個可選參數http_response_code強制將HTTP相應代碼設為指定值。 header函數中Location類型的標頭是一種特殊的header調用,常用來實現頁面跳轉。注意:

1.location和“:”號間不能有空格,否則不會跳轉。
2.在用header前不能有任何的輸出。
3.header後的PHP代碼還會被執行。例如,將浏覽器重定向到jb51.net

<?php 
 //重定向浏覽器 
header("Location: http://www.jb51.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.jb51.net)在就跳轉到register.php,如果不存在則創建cookie然後跳轉到zc.htmlfrom:http://www.jb51.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.jb51.net')
 {
  //echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: http://www.jb51.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('http://www.jb51.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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved