如何使用PHP或Javascript将网页重定向到另一个页面?

PHP is a popular programming or scripting language used to develop web pages. One of the problems developers face is using PHP to redirect the user browser to the new page. for example, we have an old URL but we want to make the transition to the new URL we can achieve this simply by using  PHP redirect.

PHP是用于开发网页的流行编程语言或脚本语言。 开发人员面临的问题之一是使用PHP将用户浏览器重定向到新页面。 例如,我们有一个旧的URL,但是我们想要过渡到新的URL,我们可以简单地通过使用PHP重定向来实现。

只需重定向 (Simply Redirect)

We will use the header function of PHP to provide a new URL to the client browser. Keep in mind that after using header function there should be no other operation that will output something or simple be sure header is the last function. In this example, we will redirect the current page to the http://www.poftut.com/newurl .

我们将使用PHP的标头功能为客户端浏览器提供新的URL。 请记住,在使用标头函数之后,不应再有其他操作将输出内容,或者简单地确保标头是最后一个函数。 在此示例中,我们将当前页面重定向到http://www.poftut.com/newurl

<?php 

/*Provide new Location with header function*/ 
header("Location:http://www.poftut.com/newurl") 
 
/*Be sure last output is from header function */ 
exit; 
?>

Please notice that we have used exit() function . This will make our page resource usage more effective.

请注意,我们已经使用了exit()函数。 这将使我们的页面资源使用更加有效。

使用JavaScript (Use JavaScript)

JavaScript is mainly client-side scripting language. Redirect operation can be done with JavaScript on the client-side by providing a new URL to the windows object’s location property like below.

JavaScript主要是客户端脚本语言。 通过向Windows对象的location属性提供新的URL,可以在客户端使用JavaScript进行重定向操作,如下所示。

window.location = "http://www.poftut.com/newurl";

Even we can make a Javascript function to use it multiple times easily

甚至我们可以使Javascript函数轻松地多次使用它

function redirect(url){ 
 
window.location=url; 
 
}

redirect("http://www.poftut.com/newurl");
LEARN MORE  Linux curl Command Tutorial With Examples
了解更多Linux curl命令示例教程

翻译自: https://www.poftut.com/how-to-redirect-web-pages-to-another-page-with-php-or-javascript/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值