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

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

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

1.header()函數(shù)

header()函數(shù)是PHP中進(jìn)行頁(yè)面跳轉(zhuǎn)的一種十分簡(jiǎn)單的方法。header()函數(shù)的主要功能是將HTTP協(xié)議標(biāo)頭(header)輸出到瀏覽器。

header()函數(shù)的定義如下:

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

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

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

<?php 
 //重定向?yàn)g覽器 
header("Location: http://www.fzitv.net"); 
 //確保重定向后,后續(xù)代碼不會(huì)被執(zhí)行 
exit;
?>

1、php跳轉(zhuǎn)代碼一句話式:

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

2、php跳轉(zhuǎn)代碼if判斷式:

復(fù)制代碼 代碼如下:

if($_COOKIE["u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');//設(shè)置cookie長(zhǎng)期有效 header('location:zc.html');

注:保存為zc.php,當(dāng)用戶訪問zc.php時(shí),判斷一個(gè)cookie是否存在,如果存(www.fzitv.net)在就跳轉(zhuǎn)到register.php,如果不存在則創(chuàng)建cookie然后跳轉(zhuǎn)到zc.htmlfrom:http://www.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}秒之后自動(dòng)跳轉(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),如果是頁(yè)面跳轉(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)到某一個(gè)頁(yè)面,推薦使用這種方法
 exit();
 }
 }
 else
 {
 @header("http/1.1 404 not found");
 @header("status: 404 not found");
 include("404.html");//跳轉(zhuǎn)到某一個(gè)頁(yè)面,推薦使用這種方法
 exit();
 }
 }

如果要做301也差不多

<?php 
 $the_host = $_SERVER['HTTP_HOST'];
 $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
 if($the_host !== 'www.fzitv.net')
 {
  //echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: http://www.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中下例代碼會(huì)報(bào)錯(cuò):
<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后不會(huì)再執(zhí)行后面的代碼.
而php在執(zhí)行header后,繼續(xù)執(zhí)行下面的代碼.
在這方面上php中的header重定向不如asp中的重定向.有時(shí)我們要重定向后,不能執(zhí)行后面的代碼:
一般地我們用
if(...)
header("...");
else
{
...
}
但是我們可以簡(jiǎn)單的用下面的方法:
if(...)
{ header("...");exit();}
還要注意的是,如果是用Unicode(UTF-8)編碼時(shí)也會(huì)出現(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://www.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)文章

最新評(píng)論

黄龙县| 和平县| 社旗县| 潮安县| 余干县| 高州市| 太湖县| 曲水县| 鹤壁市| 紫云| 张掖市| 夏津县| 镇江市| 赫章县| 渑池县| 盐边县| 潜山县| 大田县| 中方县| 城口县| 裕民县| 东海县| 郁南县| 简阳市| 塔城市| 交口县| 大埔区| 宣汉县| 敖汉旗| 治多县| 英吉沙县| 崇礼县| 南郑县| 竹山县| 呼图壁县| 英吉沙县| 灵宝市| 香河县| 东海县| 黄平县| 芜湖市|