避免重定向循环
Avoiding redirect loop
我已完成在我的网页上设置维护功能。这是index.php代码
<?php
session_start();
require_once("system/functions.php");
require_once("system/config.php");
if($maintenance == 1){
require_once(header("Location: index.php?page=maintenance"));
die();
session_destroy();
}elseif($maintenance == 0)
{
getPage();
}
?>
我也试过
header("Location: index.php?page=maintenance");
而不是上面的要求一次 header 代码。
但是如果我把
require_once("frontend/pages/maintenance.php");
它会起作用的。那么问题是人们可以在地址栏中输入他们想要的每个页面,这将显示出来。我需要它来使用它自己的 url(它适用于上面的 2 header 代码,但我收到太多重定向错误),无论如何,你都会被重定向到这个 url查看维护屏幕
maintenance.php 文件的 php 部分:
<?php
if($maintenance == 0){
header("Location: index.php?page=index");
die();
}
else{
header("Location: index.php?page=maintenance");
die();
}
?>
我可以删除 maintenance.php 文件中的其他代码部分,但它总是会重定向到 "websitename"/index.php(不过仍然是维护屏幕,与提到的问题相同以上)
所以我需要更改我的代码,以便在进行维护时,无论如何都会将您重定向到 index.php?page=maintenance。对不起,如果我错过了一些细节,已经晚了。如果需要,请随时问我:)
确实,这看起来像是在循环。当您在 index.php 脚本中时执行以下操作:
require_once(header("Location: index.php?page=maintenance"));
所以您实际上再次加载了您已经 运行 的脚本。它会再次找到 maintenance==1 并再次做完全相同的事情。
你应该只重定向一次,然后当你看到你已经在 page=maintenance URL 实际显示你想显示的维护消息时,像这样:
session_start();
require_once("system/functions.php");
require_once("system/config.php");
if($maintenance == 1){
if ($_GET['page']) == 'maintenance') {
// we have the desired URL in the browser, so now
// show appropriate maintenance page
require_once("frontend/pages/maintenance.php");
} else {
// destroy session before exiting with die():
session_destroy();
header("Location: index.php?page=maintenance");
}
die();
}
// no need to test $maintenance is 0 here, the other case already exited
getPage();
确保在 frontend/pages/maintenance.php 中 而不是 重定向到 index.php?page=maintenance 否则你仍然会陷入循环。
所以frontend/pages/maintenance.php应该是这样的:
// make sure you have not output anything yet with echo/print
// before getting at this point:
if($maintenance == 0){
header("Location: index.php?page=index");
die();
}
// "else" is not needed here: the maintenance==0 case already exited
// display the maintenance page here, but don't redirect.
echo "this is the maintenance page";
// ...
我已完成在我的网页上设置维护功能。这是index.php代码
<?php
session_start();
require_once("system/functions.php");
require_once("system/config.php");
if($maintenance == 1){
require_once(header("Location: index.php?page=maintenance"));
die();
session_destroy();
}elseif($maintenance == 0)
{
getPage();
}
?>
我也试过
header("Location: index.php?page=maintenance");
而不是上面的要求一次 header 代码。 但是如果我把
require_once("frontend/pages/maintenance.php");
它会起作用的。那么问题是人们可以在地址栏中输入他们想要的每个页面,这将显示出来。我需要它来使用它自己的 url(它适用于上面的 2 header 代码,但我收到太多重定向错误),无论如何,你都会被重定向到这个 url查看维护屏幕
maintenance.php 文件的 php 部分:
<?php
if($maintenance == 0){
header("Location: index.php?page=index");
die();
}
else{
header("Location: index.php?page=maintenance");
die();
}
?>
我可以删除 maintenance.php 文件中的其他代码部分,但它总是会重定向到 "websitename"/index.php(不过仍然是维护屏幕,与提到的问题相同以上)
所以我需要更改我的代码,以便在进行维护时,无论如何都会将您重定向到 index.php?page=maintenance。对不起,如果我错过了一些细节,已经晚了。如果需要,请随时问我:)
确实,这看起来像是在循环。当您在 index.php 脚本中时执行以下操作:
require_once(header("Location: index.php?page=maintenance"));
所以您实际上再次加载了您已经 运行 的脚本。它会再次找到 maintenance==1 并再次做完全相同的事情。
你应该只重定向一次,然后当你看到你已经在 page=maintenance URL 实际显示你想显示的维护消息时,像这样:
session_start();
require_once("system/functions.php");
require_once("system/config.php");
if($maintenance == 1){
if ($_GET['page']) == 'maintenance') {
// we have the desired URL in the browser, so now
// show appropriate maintenance page
require_once("frontend/pages/maintenance.php");
} else {
// destroy session before exiting with die():
session_destroy();
header("Location: index.php?page=maintenance");
}
die();
}
// no need to test $maintenance is 0 here, the other case already exited
getPage();
确保在 frontend/pages/maintenance.php 中 而不是 重定向到 index.php?page=maintenance 否则你仍然会陷入循环。
所以frontend/pages/maintenance.php应该是这样的:
// make sure you have not output anything yet with echo/print
// before getting at this point:
if($maintenance == 0){
header("Location: index.php?page=index");
die();
}
// "else" is not needed here: the maintenance==0 case already exited
// display the maintenance page here, but don't redirect.
echo "this is the maintenance page";
// ...