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");
翻译自: https://www.poftut.com/how-to-redirect-web-pages-to-another-page-with-php-or-javascript/