http头 - php redirect函数
如何在PHP中进行重定向? (18)
是否可以通过使用PHP将用户重定向到其他页面?
假设用户访问www.example.com/page.php
,我想将它们重定向到www.example.com/index.php
,但如何在不使用元刷新的情况下这样做? 可能?
这甚至可以保护我的网页免受未经授权的使用
1.使用header函数和
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
?>
header( 'Location: http://www.yoursite.com/new_page.html' );
你可以更新php: header
使用header()
函数发送HTTP Location
标题 :
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次执行:
[email protected]:~> 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执行。
使用echo从PHP输出JavaScript,它将完成这项工作。
echo '<script type="text/javascript">
window.location = "http://www.google.com/"
</script>';
除非您缓冲页面输出,然后再检查重定向条件,否则您无法在PHP中真正做到这一点。 这可能太麻烦了。 请记住,标题是从页面发送的第一件东西。 大部分重定向通常在页面稍后需要。 为此,您必须缓冲页面的所有输出,并稍后检查重定向条件。 此时,您可以重定向页面用户标题()或简单地回显缓冲的输出。
有关缓冲的更多信息(优点)
像其他人说的那样,发送位置标题:
header( "Location: http://www.mywebsite.com/otherpage.php" );
但是在将任何其他输出发送到浏览器之前,您需要执行此操作。
此外,如果您要使用此功能阻止未经身份验证的用户访问特定页面(请注意,有些用户代理thedailywtf.com/Articles/WellIntentioned-Destruction.aspx并继续在当前页面上进行操作,因此您需要死掉)发送后。
可能来不及回答这个问题。 不过,这是我的想法:
恕我直言,重新引导传入请求的最佳方式是使用位置标题,即可
<?php
header("Location: /index.php");
?>
一旦执行了此语句,并且输出已发出,浏览器将开始重新指导用户。 但是,确保在发送头文件之前没有任何输出(任何echo / var_dump),否则会导致错误。
虽然这是一种快速和肮脏的方式来实现原来的要求,但它最终会变成SEO灾难,因为这种重新定向总是被解释为301/302重定向,因此搜索引擎将会始终将您的索引页面视为重定向页面,而不是登录页面/主页面。 因此它会影响网站的SEO设置。
您可以使用会话变量来控制对页面的访问并授权有效的用户。
<?php
session_start();
if ( !isset( $_SESSION["valid_user"]) )
{
header("location:../index");
}
// Page goes here
?>
您可以尝试使用php头函数来进行重定向。 您将需要设置输出缓冲区,以便您的浏览器不会向屏幕发出重定向警告。
ob_start();
header("Location: ".$website);
ob_end_flush();
我已经回答了这个问题,但我会再次这样做,因为在此期间,我已经了解到,如果您在CLI中运行(重定向不能发生,因此不应该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
和307
)的问题,正如我在之前的回答的评论中提到的那样,这里是描述:
- 301 - 永久移动
- 302 - 找到
- 303 - 见其他
- 307 - 临时重定向(HTTP / 1.1)
有多种方法可以做到这一点,但如果你喜欢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
. . .
我希望你会发现一些有用的内容
参考;
现有答案总结加我自己的两分钱:
1.基本答案
您可以使用header()
函数发送新的HTTP标头,但必须在HTML或文本之前将其发送到浏览器(例如,在<!DOCTYPE ...>
声明之前)。
header('Location: '.$newURL);
2.重要细节
die()或exit()
header("Location: http://example.com/myOtherPage.php");
die();
为什么你应该使用die()
或exit()
: thedailywtf.com/Articles/WellIntentioned-Destruction.aspx
绝对网址
该网址必须是绝对的。 请参阅RFC 2616 。 但在大多数情况下,相对URL也会被接受。
状态码
PHP的“位置”标题仍然使用HTTP 302重定向代码,但这不是你应该使用的。 您应该考虑301 (永久重定向)或303 (其他)。
注意: W3C提到 303-header与“许多pre-HTTP / 1.1用户代理不兼容,目前使用的浏览器都是HTTP / 1.1用户代理,对于许多其他用户代理(如蜘蛛和机器人)来说并不是这样。
3.文件
HTTP头和PHP中的header()
函数
4.替代品
您可以使用http_redirect($url);
的替代方法http_redirect($url);
需要安装PECL包pecl 。
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/");
这些答案中的大部分都忘记了一个非常重要的步骤!
header("Location: myOtherPage.php");
die();
离开这个重要的第二条线可能会看到你最终在thedailywtf.com/Articles/WellIntentioned-Destruction.aspx 。 问题在于浏览器不必尊重页面返回的标题,因此在页眉被忽略的情况下,页面的其余部分将被执行而不用重定向。
<?php
$url = "targetpage"
Function redirect$url(){
If (headers_sent()) == false{
Echo '<script>window.location.href="' . $url . '";</script>';
}}
?>
<?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()
function Redirect($url, $permanent = false)
{
if (headers_sent() === false)
{
header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
}
exit();
}
Redirect('http://www.google.com/', false);
不要忘了死()/退出()!