PHP Header用于頁面跳轉(zhuǎn)要注意的幾個問題總結(jié)
1.header()函數(shù)
header()函數(shù)是PHP中進行頁面跳轉(zhuǎn)的一種十分簡單的方法。header()函數(shù)的主要功能是將HTTP協(xié)議標頭(header)輸出到瀏覽器。
header()函數(shù)的定義如下:
void header (string string [,bool replace [,int http_response_code]])
可選參數(shù)replace指明是替換前一條類似標頭還是添加一條相(www.dbjr.com.cn)同類型的標頭,默認為替換。
第二個可選參數(shù)http_response_code強制將HTTP相應代碼設為指定值。 header函數(shù)中Location類型的標頭是一種特殊的header調(diào)用,常用來實現(xiàn)頁面跳轉(zhuǎn)。注意:
1.location和“:”號間不能有空格,否則不會跳轉(zhuǎn)。
2.在用header前不能有任何的輸出。
3.header后的PHP代碼還會被執(zhí)行。例如,將瀏覽器重定向到jb51.net
<?php //重定向瀏覽器 header("Location: http://www.dbjr.com.cn"); //確保重定向后,后續(xù)代碼不會被執(zhí)行 exit; ?>
1、php跳轉(zhuǎn)代碼一句話式:
<?php $url = $_GET['url']; Header("Location:$url"); ?>
2、php跳轉(zhuǎn)代碼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.dbjr.com.cn)在就跳轉(zhuǎn)到register.php,如果不存在則創(chuàng)建cookie然后跳轉(zhuǎn)到zc.htmlfrom:http://www.dbjr.com.cn/phper/php-cy/62883.htm
URL重定向函數(shù)
// URL重定向 function redirect($url, $time=0, $msg=”) { //多行URL地址支持 $url = str_replace(array(“n”, “r”), ”, $url); if ( empty($msg) ) $msg = “系統(tǒng)將在{$time}秒之后自動跳轉(zhuǎn)到{$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狀態(tài),如果是頁面跳轉(zhuǎn)之后返回404狀態(tài)代碼我們可如下操作
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");//跳轉(zhuǎn)到某一個頁面,推薦使用這種方法 exit(); } } else { @header("http/1.1 404 not found"); @header("status: 404 not found"); include("404.html");//跳轉(zhuǎn)到某一個頁面,推薦使用這種方法 exit(); } }
如果要做301也差不多
<?php $the_host = $_SERVER['HTTP_HOST']; $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; if($the_host !== 'www.dbjr.com.cn') { //echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.dbjr.com.cn' . $_SERVER['PHP_SELF'] . $request_uri); } ?>
下面是和asp中重定向response.redirect的比較:
例1:
response.redirect "../test.asp"
header("location:../test.php");
兩者區(qū)別:
asp的redirect函數(shù)可以在向客戶發(fā)送頭文件后起作用.
如
<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函數(shù)之前不能向客戶發(fā)送任何數(shù)據(jù).
例2:
asp中
<html><head></head><body>
<%
response.redirect "../a.asp"
response.redirect "../b.asp"
%>
</body></html>
結(jié)果是重定向a.asp文件.
php呢?
<?
header("location:../a.php");
header("location:../b.php");
?>
<html><head></head><body></body></html>
我們發(fā)現(xiàn)它重定向b.php.
原來在asp中執(zhí)行redirect后不會再執(zhí)行后面的代碼.
而php在執(zhí)行header后,繼續(xù)執(zhí)行下面的代碼.
在這方面上php中的header重定向不如asp中的重定向.有時我們要重定向后,不能執(zhí)行后面的代碼:
一般地我們用
if(...)
header("...");
else
{
...
}
但是我們可以簡單的用下面的方法:
if(...)
{ header("...");exit();}
還要注意的是,如果是用Unicode(UTF-8)編碼時也會出現(xiàn)問題,需要調(diào)整緩存設置.
<[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.dbjr.com.cn/')!=-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>
- php用header函數(shù)實現(xiàn)301跳轉(zhuǎn)代碼實例
- 淺析php header 跳轉(zhuǎn)
- PHP利用header跳轉(zhuǎn)失效的解決方法
- php中header跳轉(zhuǎn)使用include包含解決參數(shù)丟失問題
- PHP頁面跳轉(zhuǎn)操作實例分析(header方法)
- php利用header函數(shù)實現(xiàn)文件下載時直接提示保存
- php header()函數(shù)使用說明
- PHP header()函數(shù)使用詳細(301、404等錯誤設置)
- 探討php中header的用法詳解
- PHP 常用的header頭部定義匯總
- PHP Header用于頁面跳轉(zhuǎn)時的幾個注意事項
相關(guān)文章
PHP Header用于頁面跳轉(zhuǎn)要注意的幾個問題總結(jié)
在PHP中用header("location:test.php")進行跳轉(zhuǎn)要注意以下幾點,有助于解決一些新手經(jīng)常遇到的問題2008-10-10php在apache環(huán)境下實現(xiàn)gzip配置方法
這篇文章主要介紹了php在apache環(huán)境下實現(xiàn)gzip配置方法,較為詳細的分析了相關(guān)配置文件的修改技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04