如何将一个 php 页面重定向到另一个 php 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9581268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i redirect a php page to another php page ?
提问by arunrc
Possible Duplicate:
How to redirect users to another page?
可能的重复:
如何将用户重定向到另一个页面?
I'm a beginner in the programming field....I want to redirect a php page to another one..How is it possible in the most easiest way? My concept is just to move,to the home page from the login page after doing the checking of user id and password.
我是编程领域的初学者....我想将一个 php 页面重定向到另一个页面..如何以最简单的方式实现?我的概念是在检查用户ID和密码后从登录页面移动到主页。
回答by Juvanis
<?php
header("Location: your url");
exit;
?>
回答by Hendrik Richter
While all of the other answers work, they all have one big problem: it is up to the browser to decide what to do if they encounter a Location header. Usually browser stop processing the request and redirect to the URI indicated with the Location header. But a malicious user could just ignore the Location header and continue its request. Furthermore there may be other things that cause the php interpreter to continue evaluating the script past the Location header, which is not what you intended.
虽然所有其他答案都有效,但它们都有一个大问题:如果遇到 Location 标头,则由浏览器决定该怎么做。通常浏览器会停止处理请求并重定向到由 Location 标头指示的 URI。但是恶意用户可以忽略 Location 标头并继续其请求。此外,可能还有其他原因导致 php 解释器继续评估超过 Location 标头的脚本,这不是您想要的。
Image this:
想象一下:
<?php
if (!logged_id()) {
header("Location:login.php");
}
delete_everything();
?>
What you want and expected is that not logged in users are redirected to the login page, so that only logged in users can delete_everything. But if the script gets executed past the Location header still everything gets deleted. Thus, it is import to ALWAYS put an exitafter a Location header, like this:
您想要和期望的是未登录的用户被重定向到登录页面,这样只有登录的用户才能删除所有内容。但是,如果脚本通过 Location 头执行,仍然会删除所有内容。因此,始终在 Location 标头之后放置一个出口很重要,如下所示:
<?php
if (!logged_id()) {
header("Location:login.php");
exit; // <- don't forget this!
}
delete_everything();
?>
So, to answer your question: to redirect from a php page to another page (not just php, you can redirect to any page this way), use this:
所以,要回答你的问题:从一个 php 页面重定向到另一个页面(不仅仅是 php,你可以通过这种方式重定向到任何页面),使用这个:
<?php
header("Location:http://www.example.com/some_page.php");
exit; // <- don't forget this!
?>
Small note: the HTTP standard says that you must provide absolute URLs in the Location header (http://... like in my example above) even if you just want to redirect to another file on the same domain. But in practice relative URLs (Location:some_page.php) work in all browsers, though not being standard compliant.
小提示:HTTP 标准规定,即使您只想重定向到同一域中的另一个文件,您也必须在 Location 标头中提供绝对 URL(http://... 就像我上面的示例一样)。但实际上相对 URL (Location:some_page.php) 在所有浏览器中都有效,尽管不符合标准。
回答by emre nevayeshirazi
<?php header('Location: /login.php'); ?>
The above php script redirects the user to login.php within the same site
上面的 php 脚本将用户重定向到同一站点内的 login.php
回答by MrCode
Send a Location
header to redirect. Keep in mind this only works before any other output is sent.
发送Location
标头以重定向。请记住,这仅在发送任何其他输出之前有效。
header('Location: index.php'); // redirect to index.php
回答by Rick Hoving
<?php
header('Location: http://www.google.com'); //Send browser to http://www.google.com
?>
回答by Moiz Shafqat Husain
simply you can put this and you will be redirected.
简单地你可以把这个,你会被重定向。
<?php
header("Location: your_page_name.php");
// your_page_name.php can be any page where you want to redirect
// your_page_name.php 可以是您要重定向的任何页面
?>
回答by s3polz
can use this to redirect
可以用它来重定向
echo '<meta http-equiv="refresh" content="1; URL=index.php" />';
the content=1 can be change to different value to increase the delay before redirection
content=1 可以更改为不同的值以增加重定向前的延迟