您好,欢迎访问一九零五行业门户网

php程序header location跳转注意事项

众所周知,php网站跳转有三种方法:js、html meta refresh、php header(location: $url)。但是这里有一个非常小的细节,很容易导致出错。
header(location:test.php)跳转成功除了需要注意以下三点还有一个前提必须要注意:
1、location和“:”号间不能有空格,否则会出错。
2、在用header前不能有任何的输出。
3、header后的php代码还会被执行。
这个前提就是你的编辑器是在utf-8无bom格式下编写的,而不是utf-8格式。切记!
有一次制作一个跳转程序,结果忽略了这一点,导致跳转其实都是没有成功。
程序全部源码如下,程序地址:http://www.***.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.***.com/?f=go.php">go back</a>'); }
当访问地址为:http://www.***.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><head><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></head><body><p 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=\'" + cnzz_protocol + "s9.cnzz.com/stat.php%3fid%3d5696423%26show%3dpic1\' type=\'text/javascript\'%3e%3c/script%3e"));</script></p> <script>window.location="'.$url.'";</script></body></html>'; exit(); } $url = urldecode( trim($_request['url'])); if($url) { gheader($url); } else { exit('error input,<a href="http://www.***.com/?f=go.php">go back</a>'); }
对于php跳转,我认为最好的方法就是用js+html meta。html meta可以保证访客在禁用js的情况下可以照样跳转。
以上就是php程序header location跳转注意事项的详细内容。
其它类似信息

推荐信息