Home >php教程 >php手册 >PHP重定向的具体实现方式

PHP重定向的具体实现方式

WBOY
WBOYOriginal
2016-06-13 11:05:471067browse

一、用HTTP头信息

也就是用PHP的HEADER函数。PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("Context-type: xxx/xxx"),页面的属性("No cache", "Expire")等等。

用HTTP头信息进行PHP重定向到另外一个页面的方法如下:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">url</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"www.baidu.com"</font></span><span>;  </span>
</li>
<li class="alt"><span>if (!empty($url))    </span></li>
<li class=""><span>{    </span></li>
<li class="alt"><span>    Header("HTTP/1.1 303 See Other"); //这条语句可以不写  </span></li>
<li class=""><span>    Header("Location: $url");  </span></li>
<li class="alt"><span>}    </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>    </span>
</li>
</ol>

注意一下,"Localtion:"后面有一个空格。

二、用HTML标记PHP重定向 

用HTML标记,就是用META的REFRESH标记,举例如下:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span>if (!empty($url))  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>    echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">META</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">HTTP-EQUIV</font></span><span>="Refresh" </span><span class="attribute"><font color="#ff0000">CONTENT</font></span><span>="0;</span><span class="attribute"><font color="#ff0000">URL</font></span><span>=$url"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>";  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

三、用脚本来实现PHP重定向

举例如下:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span></span></li>
<li class="">
<span></span><span class="tag-name"><strong><font color="#006699">if</font></strong></span><span> (isset($url))  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>    echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">SCRIPT</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">LANGUAGE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"JavaScript"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>";  </span>
</li>
<li class="alt">
<span>    echo "</span><span class="attribute"><font color="#ff0000">location.href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'$url'</font></span><span>";  </span>
</li>
<li class="">
<span>    echo "</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>}  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
或者

<ol class="dp-xml"><li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">script</span><span class="tag">></span></span></font></strong><span class="attribute"><font color="#ff0000">window.location</font></span><span> ="$url";</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">script</span><span class="tag">></span></font></strong><span>";</span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span></span></li></ol>


Statement:
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