提问
是否可以通过使用PHP将用户重定向到不同的页面?
假设用户转到
www.example.com/page.php
并且我想将它们重定向到www.example.com/index.php
,如何在不使用元刷新的情况下这样做?可能?这甚至可以保护我的网页免受未经授权的用户
最佳参考
现有答案摘要加上我自己的两分钱:
1。基本答案
您可以使用
header()
函数发送新的HTTP标头,但必须在任何HTML或文本之前将其发送到浏览器(例如,在<!DOCTYPE ...>
声明之前)。header('Location: '.$newURL);
2。重要细节
die()或退出()
header("Location: http://example.com/myOtherPage.php");
die();
为什么你应该使用
die()
或exit()
:每日WTF [93]绝对或相对网址
自2014年6月起,可以使用绝对和相对URL。请参阅RFC 7231,它已经取代了旧的RFC 2616,其中只允许使用绝对URL。[94] [95]
状态代码
PHPsLocation-header仍然使用HTTP 302重定向代码,但这不是您应该使用的代码。您应该考虑301(永久重定向)或303(其他)。[96] [97] [98]
注意:W3C提到303-header与许多pre-HTTP/1.1用户代理不兼容。目前使用的浏览器都是HTTP/1.1用户代理。对于蜘蛛和机器人等许多其他用户代理来说并非如此。[99]
3。文档
HTTP标头和PHP中的
header()
函数- PHP手册中的内容
- 维基百科说什么
- W3C说什么
4。替代
您可以使用
http_redirect($url);
的替代方法,该方法需要安装PECL软件包。[100] [101] [102] [103]5。辅助函数
此功能不包含303状态代码:
function Redirect($url, $permanent = false)
{
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit();
}
Redirect('http://example.com/', false);
这更灵活:
function redirect($url, $statusCode = 303)
{
header('Location: ' . $url, true, $statusCode);
die();
}
6。解决方法
如上所述
header()
重定向只能在写出任何内容之前完成工作。如果调用最小的HTML输出,它们通常会失败。然后你可以使用HTML标题解决方法(不是很专业!),如: <meta http-equiv="refresh" content="0;url=finalpage.html">
或者甚至是JavaScript重定向。
window.location.replace("http://example.com/");
其它参考1
function Redirect($url, $permanent = false)
{
if (headers_sent() === false)
{
header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
}
exit();
}
Redirect('http://www.google.com/', false);
别忘了死()/退出()!
其它参考2
使用echo从PHP输出JavaScript,这将完成工作。
echo '<script type="text/javascript">
window.location = "http://www.google.com/"
</script>';
你不能在PHP中真正做到这一点,除非你缓冲页面输出然后检查重定向条件。这可能是一个太麻烦。请记住,标题是从页面发送的第一件事。大多数重定向通常在页面后面需要。为此你必须缓冲页面的所有输出并稍后检查重定向条件。此时你可以重定向页面用户头文件()或只是回显缓冲输出。
有关缓冲的更多信息(优点)
什么是输出缓冲?
其它参考3
使用
header()
功能发送HTTP Location
标题:[106] [107]header('Location: '.$newURL);
与某些人认为相反,
die()
与重定向无关。如果您想重定向正常执行的而不是,请使用 。使用example.php:
<?php
header('Location: static.html');
$fh = fopen('/tmp/track.txt','a');
fwrite($fh, $_SERVER['REMOTE_ADDR'].' '.date('c')."\n");
fclose($fh);
?>
结果或3次执行:
bart@hal9k:~> cat /tmp/track.txt
127.0.0.1 2009-04-21T09:50:02+02:00
127.0.0.1 2009-04-21T09:50:05+02:00
127.0.0.1 2009-04-21T09:50:08+02:00
恢复—强制
die()
/exit()
是一些都市传奇,与实际PHP无关。与客户尊重Location:
标题无关。无论使用何种客户端,发送标头都不会停止PHP执行。其它参考4
的 1。在exit()
中使用标题功能
<?php
header('Location: target-page.php');
exit();
?>
但是如果你使用标题功能,那么有时你会得到警告
像标题已经发送来解决在发送标题之前不回显或打印的问题,或者你可以在标题函数之后简单地使用
die()
或exit()
。
的 2。没有标题
<?php
echo "<script>location.href='target-page.php';</script>";
?>
在这里你不会遇到任何问题
第3。使用带ob_start()
和ob_end_flush()
的标题功能
<?php
ob_start(); //this should be first line of your page
header('Location: target-page.php');
ob_end_flush(); //this should be last line of your page
?>
其它参考5
大多数答案都忘记了非常重要的一步!
header("Location: myOtherPage.php");
die();
离开那个重要的第二线可能会看到你最终在每日WTF。问题是浏览器不 以尊重页面返回的标题,因此如果忽略标题,页面的其余部分将在没有重定向的情况下执行。[108]
其它参考6
<?php header('Location: another-php-file.php'); exit(); ?>
或者如果您已经打开了php标签,请使用:
header('Location: another-php-file.php'); exit();
您还可以重定向到外部页面,例如:
header('Location: https://www.google.com'); exit();
确保包括
exit()
或include die()
其它参考7
其中许多答案都是正确的,但他们假设您有一个绝对的URL,可能不是这种情况。如果您想使用相对网址并生成其余内容,那么您可以执行以下操作...
$url = 'http://' . $_SERVER['HTTP_HOST']; // Get the server
$url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
$url .= '/your-relative/path-goes/here/'; // <-- Your relative path
header('Location: ' . $url, true, 302); // Use either 301 or 302
其它参考8
您可以使用会话变量来控制对页面的访问并授权有效用户。
<?php
session_start();
if ( !isset( $_SESSION["valid_user"]) )
{
header("location:../");
die();
}
// Page goes here
?>
http://php.net/manual/en/reserved.variables.session.php。 [109]
最近,我得到了网络攻击并决定,我需要知道用户尝试访问管理面板或保留部分Web应用程序。
所以,我在文本文件中添加了日志访问IP和用户会话,因为我不想打扰我的数据库。
其它参考9
我已经回答了这个问题,但是我会再次这样做,因为在此期间我已经了解到如果你在CLI中运行会有特殊情况(重定向不会发生,因此不应该[[t
exit()
)或者如果您的网络服务器正在运行PHP作为(F)CGI(它需要先前设置的Status
标头才能正确重定向)。function Redirect($url, $code = 302)
{
if (strncmp('cli', PHP_SAPI, 3) !== 0)
{
if (headers_sent() !== true)
{
if (strlen(session_id()) > 0) // if using sessions
{
session_regenerate_id(true); // avoids session fixation attacks
session_write_close(); // avoids having sessions lock other requests
}
if (strncmp('cgi', PHP_SAPI, 3) === 0)
{
header(sprintf('Status: %03u', $code), true, $code);
}
header('Location: ' . $url, true, (preg_match('~^30[1237]$~', $code) > 0) ? $code : 302);
}
exit();
}
}
我还处理过支持不同的HTTP重定向代码(
301
,302
,303
和307
)的问题,正如我之前的评论中提到的那样。回答,这里是描述:- 301 - 永久移动
- 302 - 找到了
- 303 - 见其他
- 307 - 临时重定向(HTTP/1.1)
其它参考10
header( 'Location: http://www.yoursite.com/new_page.html' );
其它参考11
header("Location: /index.php");
exit(0);
其它参考12
你可以使用下面的一些java脚本方法
1)self.location="http://www.example.com/index.php";
2)window.location.href="http://www.example.com/index.php";
3)document.location.href = 'http://www.example.com/index.php';
4)window.location.replace("http://www.example.com/index.php");
其它参考13
<?php
header('Location: redirectpage.php');
header('Location: redirectpage.php');exit();
echo "<script>location.href='redirectpage.php';</script>";
?>
这是常规和正常的PHP重定向,但您可以重定向一个页面,只需等待下面的代码:
<?php
header('refresh:5;url=redirectpage.php '); //Note: here 5 means 5 seconds wait for redirect.
?>
其它参考14
是的,你可以使用header()函数,[110]
header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */
exit();
并且最佳实践是在
header()
函数之后立即调用exit()函数以避免代码执行下面。 [111]根据文档,必须在发送任何实际输出之前调用
header()
。其它参考15
像其他人一样说,发送位置标题:
header( "Location: http://www.mywebsite.com/otherpage.php" );
但是在将任何其他输出发送到浏览器之前,您需要这样做。
此外,如果你打算使用它来阻止某些页面中未经过身份验证的用户,就像你提到的那样,请记住,一些用户代理会忽略这一点并继续当前页面,所以你需要死掉( )发送后。[112]
其它参考16
在语义网的前夕,正确性需要考虑。不幸的是,PHP的位置-header仍然使用HTTP 302重定向代码,严格地说,它不是重定向的最佳代码。相反应该使用的是303。[113] [114]
W3C非常友好地提到303-header与许多pre-HTTP/1.1用户代理不兼容,这相当于当前使用的浏览器。所以,302是遗物,不应该被使用。[115]
......或者你可以像其他人一样忽略它......
其它参考17
回答这个问题可能为时已晚。不过,这是我的想法:
恕我直言,重新定向传入请求的最佳方法是使用位置标头
<?php
header("Location: /index.php");
?>
执行此语句并将输出发送出去后,浏览器将开始重定向用户。
但是,确保在发送标头之前没有任何输出(任何echo/var_dump),否则会导致错误。
虽然这是一种快速而肮脏的方式来实现最初的问题,但它最终会变成SEO灾难,因为这种重新指向总是被解释为301/302重定向,因此搜索引擎将始终将您的索引页面视为重定向页面,而不是登录页面/主页面。
因此它会影响网站的SEO设置。
其它参考18
使用PHP重定向的最佳方法是以下代码...
header("Location: /index.php");
确保之后没有代码可用
header("Location: /index.php");
所有代码必须在上一行之前执行。
假设,
案例1:
echo "I am a web developer";
header("Location: /index.php");
它将正确地重定向到位置(index.php)。
案例2:
return $something;
header("Location: /index.php");
上面的代码不会重定向到该位置(index.php)。
希望很明显。
其它参考19
是的,它可以使用PHP,我们将重定向到另一个页面,尝试这个:
<?php
header("location:./");//redirect to index file
header("location:index.php");//redirect to index file
header("location:example.php");
?>
其它参考20
要将访问者重定向到另一个页面(在条件循环中特别有用),只需使用以下代码:
<?php
header('Location: mypage.php');
?>
在这种情况下,
mypage.php
是您要将访问者重定向到的页面的地址。该地址可以是绝对的,也可以包括以下格式的参数:mypage.php?param1=val1¶m2=val2)
相对/绝对路径
处理相对路径或绝对路径时,最好从服务器的根目录(DOCUMENT_ROOT)中选择绝对路径。使用以下格式:
<?php
header('Location: /directory/mypage.php');
?>
如果目标页面位于另一台服务器上,则包含完整的URL:
<?php
header('Location: http://www.ccm.net/forum/');
?>
HTTP标头
根据HTTP协议,HTTP头必须发送
before
任何类型的内容。这意味着在标题之前不应该发送任何字符 - 甚至不是空白空间!临时/永久重定向
默认情况下,上面显示的重定向类型是临时的。这意味着搜索引擎(例如Google)在编制索引时不会考虑重定向。
如果您想通知搜索引擎页面已永久移动到其他位置,请使用以下代码:
<?
header('Status: 301 Moved Permanently', false, 301);
header('Location: new_address');
?>
例如,此页面包含以下代码:
<?
header('Status: 301 Moved Permanently', false, 301);
header('Location: /pc/imprimante.php3');
exit();
?>
当您单击上面的链接时,您将自动重定向到此页面。此外,它是永久重定向(状态:301永久移动)。因此,如果您在Google中输入第一个网址,系统会自动将您重定向到第二个重定向链接。
解释PHP代码
即使访问者移动到重定向中指定的地址,服务器也会解释位于header()之后的PHP代码。在大多数情况下,这意味着您需要一种方法来遵循
exit()
函数的header()
函数,以减少服务器的负载:<?
header('Status: 301 Moved Permanently', false, 301);
header('Location: address');
exit();
?>
其它参考21
我们可以用两种方式做
- 当用户加入https://bskud.com/PINCODE/BIHAR/index.php然后重定向到https://bskud.com/PINCODE/BIHAR.php
通过以下PHP代码[116] [117]
<?php header("Location: https://bskud.com/PINCODE/BIHAR.php"); exit; ?>
保存上面的代码https://bskud.com/PINCODE/BIHAR/index.php [118]
2.当任何条件为真时,重定向到其他页面
<?php $myVar = "bskud"; if ($myVar == "bskud") { ?> <script> window.location.href="https://bskud.com"; </script> <?php } else { echo "<b>Check Website Name Again</b>"; } ?>
`
其它参考23
您可以尝试使用php标头功能进行重定向。您将需要设置输出缓冲区,以便浏览器不会向屏幕发出重定向警告。
ob_start();
header("Location: ".$website);
ob_end_flush();
其它参考24
如果你在Apache上运行,你也可以使用.htaccess进行重定向。
Redirect 301 / http://new-site.com/
其它参考25
<?php
$url = "targetpage"
Function redirect$url(){
If (headers_sent()) == false{
Echo '<script>window.location.href="' . $url . '";</script>';
}}
?>
其它参考26
有多种方法可以做到这一点,但如果您更喜欢
php
,我建议使用header()
函数。基本上
$your_target_url = “www.example.com/index.php”;
header(“Location : $your_target_url”);
exit();
如果你想提高一个档次,最好在功能中使用它,这样,你就可以在其中添加身份验证和其他检查元素。
让我们通过检查用户的级别来尝试。
因此,假设您已将用户的权限级别存储在名为
u_auth
的会话中。在
function.php
中<?php
function authRedirect($get_auth_level, $required_level, $if_fail_link = “www.example.com/index.php”){
if($get_auth_level != $required_level){
header(location : $if_fail_link);
return false;
exit();
}else{
return true;
}
}
. . .
然后,您将为要进行身份验证的每个页面调用该函数。
如
page.php
或任何其他页面。<?php
// page.php
require “function.php”
authRedirect($_SESSION[‘u_auth’], 5); // redirects to www.example.com/index.php if the user isn’t auth level 5
authRedirect($_SESSION[‘u_auth’], 4); // redirects to www.example.com/index.php if the user isn’t auth level 4
authRedirect($_SESSION[‘u_auth’], 2, “www.someotherplace.com/somepage.php”); // redirects to www.someotherplace.com/somepage.php if the user isn’t auth level 2
. . .
我希望你能找到一些有用的内容
参考;
- http://php.net/manual/en/function.header.php
其它参考27
1。使用标头内置php函数 [120]
a)没有参数的简单重定向
<?php
header('Location: index.php');
?>
b)使用GET参数重定向
<?php
$id = 2;
header("Location: index.php?id=$id&msg=succesfully redirect");
?>
2。使用php中的javascript重定向
a)没有参数的简单重定向
<?php
echo "<script>location.href='index.php';</script>";
?>
b)使用GET参数重定向
<?php
$id = 2;
echo "<script>location.href='index.php?id=$id&msg=succesfully redirect';</script>";
?>