Specific tips for using headers in PHP_PHP Tutorial

WBOY
Release: 2016-07-15 13:28:51
Original
809 people have browsed it

We cannot have spaces between 1, location and the ":" sign, otherwise an error will occur.

2. There cannot be any output before using the header.

3. The PHP code after using header in PHP will still 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 client.

Such as

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><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></font></strong><span> </span></span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>%response.redirect "../test.asp"%</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><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></font></strong><span> </span>
</li>
</ol>
Copy after login

Check if the following code in header is used in PHP, an error will be reported:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><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></font></strong><span> </span></span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>header</FONT></STRONG></SPAN><SPAN>("location:../test.php");  </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><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></font></strong><span> </span>
</li>
</ol>
Copy after login

It can only be like this:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>header</FONT></STRONG></SPAN><SPAN>("location:../test.php");  </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span> </span></span></li>
<li class="">
<span></span><strong><font color="#006699"><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></font></strong><span>...</span><strong><font color="#006699"><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></font></strong><span> </span>
</li>
</ol>
Copy after login

That is, no data can be sent to the client before using the header function in PHP.

Example 2:

asp

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><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></font></strong><span> </span></span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>%  </SPAN></SPAN><LI class=alt><SPAN>response.redirect "../a.asp"  </SPAN><LI class=""><SPAN>response.redirect "../b.asp"  </SPAN><LI class=alt><SPAN>%</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><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></font></strong><span> </span>
</li>
</ol>
Copy after login

The result is to redirect the a.asp file.

What about php?

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>header</FONT></STRONG></SPAN><SPAN>("location:../a.php");  </SPAN></SPAN><LI class=alt><SPAN>header("location:../b.php");  </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span> </span></span></li>
<li class="alt">
<span></span><strong><font color="#006699"><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 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></font></strong><span> </span>
</li>
</ol>
Copy after login

We found that it redirects b.php.

It turns out that redirect is performed in asp The following code will not be executed.

After executing the header, php will continue to execute the following code.

In this regard, the header redirection in php is not as good as the redirection in asp. Sometimes we have to redirect After that, the following code cannot be executed:

Generally we use >

But we can simply use the following method:

<ol class="dp-xml">
<li class="alt"><span><span>if(...)  </span></span></li>
<li class=""><span>header("...");  </span></li>
<li class="alt"><span>else  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>...  </span></li>
<li class=""><span>}  </span></li>
</ol>
Copy after login

One thing to note when using headers in PHP is that problems will also occur if you use Unicode (UTF-8) encoding, and you need to adjust the cache settings.

<ol class="dp-xml">
<li class="alt"><span><span>if(...)  </span></span></li>
<li class=""><span>{ header("...");exit();}  </span></li>
</ol>
Copy after login

http://www.bkjia.com/PHPjc/446416.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/446416.html
<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>[</SPAN><SPAN class=attribute><FONT color=#ff0000>email</FONT></SPAN><SPAN>=%@]%@</SPAN><SPAN class=attribute><FONT color=#ff0000>LANGUAGE</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"VBSCRIPT[/email]"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>CODEPAGE</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"936"</FONT></SPAN><SPAN>%</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span></span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>%if Request.ServerVariables("SERVER_NAME")="s.jb51.net" then  </SPAN></SPAN><LI class=alt><SPAN>response.redirect "news/index.htm"  </SPAN><LI class=""><SPAN>else%</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>%end if%</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>script</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="alt">
<span>var </span><span class="attribute"><font color="#ff0000">url</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">location</font></span><span>.href;  </span>
</li>
<li class="">
<span>if(url.indexOf('http://www.jb51.net/')!=-1)</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'/index/index.htm'</font></span><span>;  </span>
</li>
<li class="alt">
<span>if(url.indexOf('http://www.kanshule.com/')!=-1)</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'/index1/index.htm'</font></span><span>;  </span>
</li>
<li class="">
<span>if(url.indexOf('http://www.shouji17.com/')!=-1)</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'/cn/index.asp'</font></span><span>;  </span>
</li>
<li class="alt">
<span>if(url.indexOf('http://www.baidu.com/')!=-1)</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'/cn/index.asp'</font></span><span>;  </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>script</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
</ol>
Copy after login

TechArticle

We cannot have spaces between 1, location and ":", otherwise an error will occur. 2. There cannot be any output before using the header. 3. The PHP code after using header in PHP will still be executed. Below is...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!