【问题标题】:How do i get if a boolean is set to true in a config? [closed]如果在配置中将布尔值设置为 true,我如何获得? [关闭]
【发布时间】:2013-11-03 15:00:22
【问题描述】:

我正在制作维护页面,您可以在 PHP 中将维护设置为 true,如下所示:

$maintenance = false;

我希望每个页面都包含一个函数,就像这样

include 'example.php';
maintenance_mode();

并且该功能应该将用户重定向到维护页面。

【问题讨论】:

  • 你的问题是?
  • function maintenance_mode(){global $maintenance; if ($maintenance){header('Location: redirect.php');exit;} 就是这样。 (警告:这也是非常糟糕的代码)
  • 为什么需要变量和函数?只需在函数中重定向

标签: php maintenance


【解决方案1】:
   function isMaintenance($maintenance)
   {
     if(isset($maintenance) && $maintenance){
        header("location:maintenace_page.php");
        exit();
     }          
   }

在 config.php 中添加 $maintenance 变量,

  $maintenance =true;

并在相应页面中包含 isMaintenance($maintenance) 功能页面和配置页面,或者如果您已经在所有页面中包含任何公共文件,则使用该文件并将提及的内容添加到该页面中。

【讨论】:

  • 如果你复制我的代码,你不应该忘记die 语句,它是正常工作的必要条件。您的代码仍会呈现整个网站。
  • 将变量传递给该函数并不比查询全局变量的函数好。将维护变量定义为常量define('IS_MAINTENANCE', 1);
猜你喜欢
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
相关资源
最近更新 更多