最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP Header用于頁面跳轉(zhuǎn)要注意的幾個問題總結(jié)

 更新時間:2008年10月03日 14:50:06   投稿:mdxy-dxy  
在PHP中用header("location:test.php")進行跳轉(zhuǎn)要注意以下幾點,有助于解決一些新手經(jīng)常遇到的問題

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指明是替換前一條類似標頭還是添加一條相(m.fzitv.net)同類型的標頭,默認為替換。

第二個可選參數(shù)http_response_code強制將HTTP相應(yīng)代碼設(shè)為指定值。 header函數(shù)中Location類型的標頭是一種特殊的header調(diào)用,常用來實現(xiàn)頁面跳轉(zhuǎn)。注意:

1.location和“:”號間不能有空格,否則不會跳轉(zhuǎn)。
2.在用header前不能有任何的輸出。
3.header后的PHP代碼還會被執(zhí)行。例如,將瀏覽器重定向到j(luò)b51.net

<?php 
 //重定向瀏覽器 
header("Location: http://m.fzitv.net"); 
 //確保重定向后,后續(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');//設(shè)置cookie長期有效 header('location:zc.html');

注:保存為zc.php,當用戶訪問zc.php時,判斷一個cookie是否存在,如果存(m.fzitv.net)在就跳轉(zhuǎn)到register.php,如果不存在則創(chuàng)建cookie然后跳轉(zhuǎn)到zc.htmlfrom:http://m.fzitv.net/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 !== 'm.fzitv.net')
 {
  //echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: http://m.fzitv.net' . $_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)整緩存設(shè)置.
<[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://m.fzitv.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>

相關(guān)文章

最新評論

广安市| 威海市| 台安县| 上虞市| 剑川县| 都昌县| 定结县| 伊川县| 秀山| 博湖县| 封开县| 清水河县| 三台县| 石门县| 鸡西市| 山丹县| 常宁市| 湖北省| 隆化县| 梁山县| 沙田区| 富锦市| 潮安县| 青神县| 韩城市| 炎陵县| 布拖县| 元阳县| 女性| 梁河县| 陆川县| 招远市| 德江县| 辽宁省| 宝丰县| 北安市| 双城市| 特克斯县| 娱乐| 河源市| 寿宁县|