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

PHP重定向的具体实现方式

WBOY
WBOY原創
2016-06-13 11:05:471062瀏覽

一、用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>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn