php开发中的页面跳转方法总结,php开发页面跳转_PHP教程
Jul 13, 2016 am 09:56 AMphp开发中的页面跳转方法总结,php开发页面跳转
页面跳转可能是由于用户单击链接、按钮等触发的,也可能是系统自动产生的。页面自动跳转在WEB开发中经常用到,而且根据需求可以采用不同的跳转方式,比如提示操作信息后延时跳转等, 本文总结了WEB开发中常见的几种页面跳转方法。
PHP header()函数跳转
PHP的header()函数非常强大,其中在页面url跳转方面也调用简单,使用header()直接跳转到指定url页面,这时页面跳转是302重定向:
$url = "http://www.bkjia.com/"; header( "Location: $url" );
我们有可能会遇到特殊的跳转时,比如网站改版有个页面地址要做301重定向,当然你可以通过web配置rewrite来实现,但现在我要告诉大家,可以使用php的header()函数做301跳转,代码如下:
//301跳转 header( "HTTP/1.1 301 Moved Permanently" ); header( "Location: $url" );
Meta设置跳转
html中的meta信息里可以直接设置跳转,可以设置跳转延迟时间和跳转url,经常应用,比如支付完了告诉用户支付成功并跳转到订单页面,代码非常简单,就在
中加上一句:<meta http-equiv="refresh" content="5;url=http://www.bkjia.com">
以上代码表示在5秒钟后页面会自动跳转到http://www.bkjia.com。
Javascript跳转
Javascript跳转也非常简单,直接一句话:
<script> window.location.href="http://www.bkjia.com"; </script>
注意,上面这段代码直接跳转后,在目标页面地址中是获取不到来路(referer,也叫来源)的,在实际项目中,遇到有客户要求跳转要带来路(即目标网页可以获取到页面从哪里跳转来的),这时,我们可以用javascript模拟一次点击,然后跳转就满足了客户的需求。
<script> //有来路 var aa = document.createElement("a"); aa.setAttribute("href","http://www.bkjia.com"); var bodys=document.getElementsByTagName("body")[0]; bodys.appendChild(aa); aa.click(); </script>
当然,实际开发中我们可以把后面两种方式整合到PHP中去,便于适用各种跳转需求。
以上所述就是本文的全部内容了,希望大家能够喜欢。

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Comment configurer Visual Studio Code (VS Code) pour le développement PHP
