When we jump to a page, we usually use
Be careful when using header("location:test.php") in PHP to jump. The following points will help solve some problems that novices often encounter
1. There cannot be a space between location and the ":" sign, 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 following is a comparison with response.redirect in asp:
Example 1:
response.redirect "../test.asp"
header( "location:../test.php");
The difference between the two:
asp’s redirect function can work after sending the header file to the customer.
such as
1 2 3 4 5 6 7 8 9 | <ol class = "dp-xml" >
<li class = "alt" ><span><span class = "tag" >< </span><span class = "tag-name" >html</span><span class = "tag" >></span><span class = "tag" >< </span><span class = "tag-name" >head</span><span class = "tag" >></span><span class = "tag" >< /</span><span class = "tag-name" >head</span><span class = "tag" >></span><span class = "tag" >< </span><span class = "tag-name" >body</span><span class = "tag" >></span><span> </span></span></li>
<li>
<span class = "tag" >< </span><span>%response.redirect "../test.asp" %</span><span class = "tag" >></span><span> </span>
</li>
<li class = "alt" >
<span class = "tag" >< /</span><span class = "tag-name" >body</span><span class = "tag" >></span><span class = "tag" >< /</span><span class = "tag-name" >html</span><span class = "tag" >></span><span> </span>
</li>
</ol>
|
Copy after login
The following example code in php will report an error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <ol class = "dp-xml" >
<li class = "alt" ><span><span class = "tag" ><</span><span> </span><span class = "tag-name" >html</span><span class = "tag" >></span><span class = "tag" ><</span><span> </span><span class = "tag-name" >head</span><span class = "tag" >></span><span class = "tag" ><</span><span> /head</span><span class = "tag" >></span><span class = "tag" ><</span><span> </span><span class = "tag-name" >body</span><span class = "tag" >></span><span> </span></span></li>
<li>
<span class = "tag" ><?</span><span> </span></li><li class = "alt" ><span class = "tag-name" >header</span><span>( "location:../test.php" ); </span></li><li><span class = "tag" >?></span><span> </span>
</li>
<li class = "alt" >
<span class = "tag" ><</span><span> /body</span><span class = "tag" >></span><span class = "tag" ><</span><span> /html</span><span class = "tag" >></span><span> </span>
</li>
<li><span>只能这样: </span></li>
<li class = "alt" >
<span class = "tag" ><</span><span> ? </span></li><li><span>header( "location:../test.php" ); </span></li><li class = "alt" ><span class = "tag" >?></span><span> </span>
</li>
<li>
<span class = "tag" ><</span><span> </span><span class = "tag-name" >html</span><span class = "tag" >></span><span class = "tag" ><</span><span> </span><span class = "tag-name" >head</span><span class = "tag" >></span><span class = "tag" ><</span><span> /head</span><span class = "tag" >></span><span class = "tag" ><</span><span> </span><span class = "tag-name" >body</span><span class = "tag" >></span><span>...</span><span class = "tag" ><</span><span> /body</span><span class = "tag" >></span><span class = "tag" ><</span><span> /html</span><span class = "tag" >></span><span> </span>
</li>
</ol>
|
Copy after login
That is, the PHP header cannot send any data to the customer before.
Example 2:
in asp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <ol class = "dp-xml" >
<li class = "alt" ><span><span class = "tag" ><</span><span> </span><span class = "tag-name" >html</span><span class = "tag" >></span><span class = "tag" ><</span><span> </span><span class = "tag-name" >head</span><span class = "tag" >></span><span class = "tag" ><</span><span> /head</span><span class = "tag" >></span><span class = "tag" ><</span><span> </span><span class = "tag-name" >body</span><span class = "tag" >></span><span> </span></span></li>
<li>
<span class = "tag" ><</span><span>% </span></li><li class = "alt" ><span>response.redirect "../a.asp" </span></li><li><span>response.redirect "../b.asp" </span></li><li class = "alt" ><span>%</span><span class = "tag" >></span><span> </span>
</li>
<li>
<span class = "tag" ><</span><span> /body</span><span class = "tag" >></span><span class = "tag" ><</span><span> /html</span><span class = "tag" >></span><span> </span>
</li>
<li class = "alt" ><span>结果是重定向a.asp文件. </span></li>
<li><span>php呢? </span></li>
<li class = "alt" >
<span class = "tag" ><</span><span> ? </span></li><li><span>header( "location:../a.php" ); </span></li><li class = "alt" ><span>header( "location:../b.php" ); </span></li><li><span class = "tag" >?></span><span> </span>
</li>
<li class = "alt" >
<span class = "tag" ><</span><span> </span><span class = "tag-name" >html</span><span class = "tag" >></span><span class = "tag" ><</span><span> </span><span class = "tag-name" >head</span><span class = "tag" >></span><span class = "tag" ><</span><span> /head</span><span class = "tag" >></span><span class = "tag" ><</span><span> </span><span class = "tag-name" >body</span><span class = "tag" >></span><span class = "tag" ><</span><span> /body</span><span class = "tag" >></span><span class = "tag" ><</span><span> /html</span><span class = "tag" >></span><span> </span>
</li>
</ol>
|
Copy after login
We found that it redirects b.php.
It turns out that after executing redirect in asp, the following code will not be executed.
After executing the header, php continues to execute the following Code.
In this regard, header redirection in php is not as good as redirection in asp. Sometimes we cannot execute the subsequent code after redirection:
Generally we use
1 2 3 4 5 6 7 8 | <ol class = "dp-xml" >
<li class = "alt" ><span><span> if (...) </span></span></li>
<li><span>header( "..." ); </span></li>
<li class = "alt" ><span> else </span></li>
<li><span>{ </span></li>
<li class = "alt" ><span>... </span></li>
<li><span>} </span></li>
</ol>
|
Copy after login
But we can simply use the following PHP header method:
1 2 3 4 | <ol class = "dp-xml" >
<li class = "alt" ><span><span> if (...) </span></span></li>
<li><span>{ header( "..." ); exit ();} </span></li>
</ol>
|
Copy after login
Also note that problems will occur if Unicode (UTF-8) is used to encode, and the cache of the PHP header needs to be adjusted. Settings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <ol class = "dp-xml" >
<li class = "alt" ><span><span class = "tag" ><</span><span> [</span><span class = "attribute" >email</span><span>=%@]%@</span><span class = "attribute" >LANGUAGE</span><span>=</span><span class = "attribute-value" > "VBSCRIPT[/email]" </span><span> </span><span class = "attribute" >CODEPAGE</span><span>=</span><span class = "attribute-value" > "936" </span><span>%</span><span class = "tag" >></span><span> </span></span></li>
<li>
<span class = "tag" ><</span><span> % if Request.ServerVariables( "SERVER_NAME" )= "s.jb51.net" then </span></li><li class = "alt" ><span>response.redirect "news/index.htm" </span></li><li><span> else %</span><span class = "tag" >></span><span> </span>
</li>
<li class = "alt" >
<span class = "tag" ><</span><span> % end if %</span><span class = "tag" >></span><span> </span>
</li>
<li>
<span class = "tag" ><</span><span> </span><span class = "tag-name" >script</span><span class = "tag" >></span><span> </span>
</li>
<li class = "alt" >
<span> var </span><span class = "attribute" >url</span><span> = </span><span class = "attribute-value" >location</span><span>.href; </span>
</li>
<li>
<span> if (url.indexOf( 'http://www.jb51.net/' )!=-1)</span><span class = "attribute" >location.href</span><span>=</span><span class = "attribute-value" > '/index/index.htm' </span><span>; </span>
</li>
<li class = "alt" >
<span> if (url.indexOf( 'http://www.kanshule.com/' )!=-1)</span><span class = "attribute" >location.href</span><span>=</span><span class = "attribute-value" > '/index1/index.htm' </span><span>; </span>
</li>
<li>
<span> if (url.indexOf( 'http://www.shouji17.com/' )!=-1)</span><span class = "attribute" >location.href</span><span>=</span><span class = "attribute-value" > '/cn/index.asp' </span><span>; </span>
</li>
<li class = "alt" >
<span> if (url.indexOf( 'http://www.baidu.com/' )!=-1)</span><span class = "attribute" >location.href</span><span>=</span><span class = "attribute-value" > '/cn/index.asp' </span><span>; </span>
</li>
<li>
<span class = "tag" ><</span><span> /script</span><span class = "tag" >></span><span> </span>
</li>
</ol>
|
Copy after login
The above are some issues that should be paid attention to when using PHP header.
http://www.bkjia.com/PHPjc/446162.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446162.htmlTechArticleWhen we jump to a page, we usually use header(location:test.php in PHP ) When jumping, please pay attention to the following points, which will help solve some problems that novices often encounter 1. l...