Heim  >  Artikel  >  php教程  >  使用PHP程序header location 进行跳转的注意的一个细节!

使用PHP程序header location 进行跳转的注意的一个细节!

WBOY
WBOYOriginal
2016-06-06 19:49:24829Durchsuche

众所周知,PHP网站跳转有三种方法:JS、HTML META Refresh、PHP header(location: $url)。但是这里有一个非常小的细节,很容易导致出错。 有一次制作一个跳转程序,结果忽略了这一点,导致跳转其实都是没有成功。 程序全部源码如下,程序地址:http://www.35

众所周知,PHP网站跳转有三种方法:JS、HTML META Refresh、PHP header("location: $url")。但是这里有一个非常小的细节,很容易导致出错。

有一次制作一个跳转程序,结果忽略了这一点,导致跳转其实都是没有成功。


程序全部源码如下,程序地址:http://www.35dalu.com/go.php

<?php error_reporting(7);
$url = urldecode( trim($_REQUEST['url']));
if($url)
{
    header("Location: $url");

} else {

    exit('Error Input,<a href="http://www.35dalu.com/?f=go.php">go back');
}

当访问地址为:http://www.35dalu.com/go.php?url=http%3A%2F%2Fwww.zbphp.com%2F 的时候,firefox浏览器是正常的。后来把这个跳转程序复制到公司的另外一个网站,让QQ上的一些好友测试,结果很多人都说打不开:IE内核的浏览器直接提示无法访问或者找不到,chrome有时候会提示被重置或找不到,使用firefox测试也偶偶会提示无法找到,但多刷新一次才显示正常。


仔细检查代码,是没有问题的。况且firefox是可以跳转,后想到了以往阅读到的一点就是:IE浏览器如果输出的内容字节太小(小于512字节),那么就会被忽略。然后将源码由header location跳转修改为 js html才所有浏览器都测试通过,原先偶偶出现firefox点击提示找不到xxx服务器也没有再出现过,现go.php全部源码:

<?php error_reporting(7);
function gheader($url)
{
echo '<html><meta http-equiv="Content-Language" content="zh-CN"><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><meta http-equiv="refresh" content="0;url='.$url.'"><title>loading ... </title><div style="display:none">
<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id=\'cnzz_stat_icon_5696423\'%3E%3C/span%3E%3Cscript src=\'" &#43; cnzz_protocol &#43; "s9.cnzz.com/stat.php%3Fid%3D5696423%26show%3Dpic1\' type=\'text/javascript\'%3E%3C/script%3E"));</script>
</div>
<script>window.location="'.$url.'";</script>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn