Home > Article > Backend Development > PHP program header location jump precautions
As we all know, there are three ways to jump to PHP websites: JS, HTML META Refresh, and PHP header("location: $url"). But there is a very small detail here that can easily lead to errors.
Header("location:test.PHP") successfully jumps. In addition to the following three points, there is also a prerequisite that must be paid attention to:
1, location and ":" sign There must be no spaces between them, otherwise an error will occur.
2. There cannot be any output before using the header.
3. The php code after the header will also be executed.
The premise is that your editor is written in UTF-8 BOM-free format, not UTF-8 format. Remember!
Once I made a jump program, but I ignored this point, causing the jump to be unsuccessful.
The entire source code of the program is as follows, program address: 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>'); }
When the access address is: http://www. ***.com/go.php?url=http%3A%2F%2Fwww.zbphp.com%2F, the firefox browser is normal. Later, I copied this jump program to another website of the company and asked some friends on QQ to test it. As a result, many people said that it could not be opened: the IE-based browser directly prompts that it cannot be accessed or cannot be found, and chrome sometimes prompts It is reset or cannot be found. When testing with Firefox, it will occasionally prompt that it cannot be found, but it will display normally after refreshing one more time.
Check the code carefully, there is no problem. Besides, firefox can jump, and then I thought of something I read in the past: IE browserIf the output content bytes are too small (less than 512 bytes), Then it will be ignored. Then the source code was changed from header location to js html before all browsers passed the test. The firefox click prompt cannot find the xxx server did not appear again. Now it appears. Go.php full source code:
<?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>'); }
For PHP jump, I think the best way is to use JS+HTML META. HTML META can ensure that visitors can still jump even if JS is disabled.
The above is the detailed content of PHP program header location jump precautions. For more information, please follow other related articles on the PHP Chinese website!