Home > php教程 > php手册 > 为你详解PHP页面跳转函数的具体含义

为你详解PHP页面跳转函数的具体含义

WBOY
Release: 2016-06-13 11:09:17
Original
1130 people have browsed it

对于

PHP本身没有一个完全意义上的PHP页面跳转函数,也许Header函数算一个,但是,它只能用于页面的第一行。如果放在PHP页面的最后位置,除非前面的PHP没有输出任何字符,否则,会报错。

下面提供三种PHP自动页面跳转的方法,其一就是使用Header函数,其二则是使用HTML固有标记,当然,这个方法不仅仅可以应用于PHP,同样可以应用于ASP,.Net,以及Jsp中,第三种方法是输出Javascript,利用Js代码,达到PHP页面自动跳转的目的,同样的,这种方法也同样适用于PHP以外的其他语言,只是相应语言代码不同而已。

PHP页面跳转函数一、用HTTP头信息(Header函数)

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

<ol class="dp-xml">
<li class="alt"><span class="tag"> ?PHP  </span></li>
<li>
<span>$</span><span class="attribute">url</span><span> = </span><span class="attribute-value">czbin</span><span>.</span>PHP<span>  </span>
</li>
<li class="alt"><span>Header("HTTP/1.1 303 See Other");  </span></li>
<li><span>Header("Location: $url");  </span></li>
<li class="alt"><span>exit;   </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

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

PHP页面跳转函数二、利用HTML标记(META中的REFRESH属性)

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

<ol class="dp-xml">
<li class="alt"><span class="tag"> ?PHP<span> $</span><span class="attribute">url</span><span> = </span><span class="attribute-value">czbin</span><span>.</span>PHP<span>;</span><span>?> </span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">HTML</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">HEAD</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">META</span><span> </span><span class="attribute">HTTP-EQUIV</span><span>=</span><span class="attribute-value">"REFRESH"</span><span> <br></span><span class="attribute">CONTENT</span><span>="10; </span><span class="attribute">URL</span><span>=</span><span class="tag"><span> ? echo $url;</span><span class="tag">?></span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span class="tag"><span> /HEAD</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">BODY</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span class="tag"><span> /BODY</span><span class="tag">></span><span> </span></span></li>
<li><span class="tag"><span> /HTML</span><span class="tag">></span><span> </span></span></li>
</ol>
Copy after login

注:这里的CONTENT="10 的意思是说10秒以后跳转。

PHP页面跳转函数三、用JAVASCRIPT脚本来实现

举例如下:
 

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span>PHP<span><span> </span></span></span></li>
<li>
<span>$</span><span class="attribute">url</span><span>=</span><span class="attribute-value">czbin</span><span>.</span>PHP<span>;  </span>
</li>
<li class="alt"><span>echo "<span class="tag"><span class="tag-name">SCRIPT</span><span> </span><span class="attribute">LANGUAGE</span><span>=</span><span class="attribute-value">"JavaScript"</span><span class="tag">></span><span>";  </span></span></span></li>
<li>
<span>echo "</span><span class="attribute">location.href</span><span>=</span><span class="attribute-value">'$url'</span><span>";  </span>
</li>
<li class="alt">
<span>echo "</span><span class="tag"><span class="tag-name">SCRIPT</span><span class="tag">></span><span>--</span><span class="tag">></span><span>";  </span></span>
</li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template