Php 如何在';维护模式';在Codeigniter中?
我使用的是最新的codeigniter,我需要创建一个标志(最好是在配置中),当设置为“true”时,所有页面都会显示一条“维护模式”消息,而不是执行它们的控制器代码Php 如何在';维护模式';在Codeigniter中?,php,codeigniter,Php,Codeigniter,我使用的是最新的codeigniter,我需要创建一个标志(最好是在配置中),当设置为“true”时,所有页面都会显示一条“维护模式”消息,而不是执行它们的控制器代码 执行此操作的最佳/最简单做法是什么?通过在核心目录中放置一个名为MY_Controller的新文件来扩展CI_控制器 在该文件的构造函数中,执行以下操作: public function __construct() { parent::__construct(); if($this->config->
执行此操作的最佳/最简单做法是什么?通过在核心目录中放置一个名为MY_Controller的新文件来扩展CI_控制器 在该文件的构造函数中,执行以下操作:
public function __construct()
{
parent::__construct();
if($this->config->item('maintenance_mode') == TRUE) {
$this->load->view('maintenance_view');
die();
}
}
让应用程序中的所有控制器都从该类继承。这样如何:
class Maintenance_mode {
function __construct(){
$CI =& get_instance();
$check_maintenance = $CI->db->select('flag_mode')->get('tbl_settings')->result();
if($check_maintenance[0]->flag_mode == '1')
redirect(site_url('maintenance_mode_controller'));
}
}
下一步是创建用于维护的控制器页面。以下是我为创建维护模式而想到的方法
这里是我的解决方案,对我来说很好: 下面的代码将立即调用maintanance.php,这样您就可以在世界看不到的情况下继续破解CI代码 还允许您添加自己的ip地址,以便您仍然可以访问站点进行测试等 在index.php中,在顶部添加:
$maintenance = false; ## set to true to enable
if ($maintenance)
{
if (isset( $_SERVER['REMOTE_ADDR']) and $_SERVER['REMOTE_ADDR'] == 'your_ip')
{
##do nothing
} else
{
error_reporting(E_ALL);
ini_set('display_errors', 1); ## to debug your maintenance view
require_once 'maintenance.php'; ## call view
return;
exit();
}
}
在与index.php相同的文件夹中添加文件Maintanance.php(或上面的更新路径):
维修
身体{
宽度:500px;
保证金:0自动;
文本对齐:居中;
颜色:蓝色;
}
很抱歉在升级过程中给您带来不便
请稍后再访问
这个很好用
application/views/vw_maintenance.php
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
<style>Style your page</style>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
<?php exit(); ?>
class Maintenance{
private $CI;
public function __construct() {
$this->CI =& get_instance();
// flag on and off
$this->flag( $this->CI->uri->segment(1) );
// get the flag status
$check_maintenance = $this->CI->db->select('flag_mode')->where(array('setting_name' => 'maintenance'))->get('tbl_settings')->result();
//display view if true
if($check_maintenance[0]->flag_mode == '1')
$this->CI->load->view('vw_maintenance');
}
private function flag($command){
$this->CI->db->where('setting_name', 'evolving');
switch($command){
case "activate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 1) );
break;
case "deactivate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 0) );
redirect(site_url('/'));
break;
}
}
}
$config['maintenance_mode'] = TRUE;
$config['maintenance_ips'] = array('0.0.0.0', '1.1.1.1', '2.2.2.2');
if(!in_array($_SERVER['REMOTE_ADDR'], $this->config->item('maintenance_ips')) && $this->config->item('maintenance_mode')) {
$route['default_controller'] = "your_controller/maintenance";
$route['(:any)'] = "your_controller/maintenance";";
}
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
自动加载库,以便在每次加载页面时都能进行检查
现在,您可以通过键入或来激活和停用维护模式。这里是我的解决方案,简单、干净、有效地用于所有URL调用和SEO方面:
将此变量添加到: application/config/config.php
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
<style>Style your page</style>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
<?php exit(); ?>
class Maintenance{
private $CI;
public function __construct() {
$this->CI =& get_instance();
// flag on and off
$this->flag( $this->CI->uri->segment(1) );
// get the flag status
$check_maintenance = $this->CI->db->select('flag_mode')->where(array('setting_name' => 'maintenance'))->get('tbl_settings')->result();
//display view if true
if($check_maintenance[0]->flag_mode == '1')
$this->CI->load->view('vw_maintenance');
}
private function flag($command){
$this->CI->db->where('setting_name', 'evolving');
switch($command){
case "activate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 1) );
break;
case "deactivate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 0) );
redirect(site_url('/'));
break;
}
}
}
$config['maintenance_mode'] = TRUE;
$config['maintenance_ips'] = array('0.0.0.0', '1.1.1.1', '2.2.2.2');
if(!in_array($_SERVER['REMOTE_ADDR'], $this->config->item('maintenance_ips')) && $this->config->item('maintenance_mode')) {
$route['default_controller'] = "your_controller/maintenance";
$route['(:any)'] = "your_controller/maintenance";";
}
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
将此条件添加到以下内容的末尾: application/config/routes.php
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
<style>Style your page</style>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
<?php exit(); ?>
class Maintenance{
private $CI;
public function __construct() {
$this->CI =& get_instance();
// flag on and off
$this->flag( $this->CI->uri->segment(1) );
// get the flag status
$check_maintenance = $this->CI->db->select('flag_mode')->where(array('setting_name' => 'maintenance'))->get('tbl_settings')->result();
//display view if true
if($check_maintenance[0]->flag_mode == '1')
$this->CI->load->view('vw_maintenance');
}
private function flag($command){
$this->CI->db->where('setting_name', 'evolving');
switch($command){
case "activate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 1) );
break;
case "deactivate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 0) );
redirect(site_url('/'));
break;
}
}
}
$config['maintenance_mode'] = TRUE;
$config['maintenance_ips'] = array('0.0.0.0', '1.1.1.1', '2.2.2.2');
if(!in_array($_SERVER['REMOTE_ADDR'], $this->config->item('maintenance_ips')) && $this->config->item('maintenance_mode')) {
$route['default_controller'] = "your_controller/maintenance";
$route['(:any)'] = "your_controller/maintenance";";
}
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
在中创建方法维护: 应用程序/controllers/your_controller.php
function maintenance() {
$this->output->set_status_header('503');
$this->load->view('maintenance_view');
}
创建视图: application/views/maintenance\u view.php
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
<style>Style your page</style>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
<?php exit(); ?>
class Maintenance{
private $CI;
public function __construct() {
$this->CI =& get_instance();
// flag on and off
$this->flag( $this->CI->uri->segment(1) );
// get the flag status
$check_maintenance = $this->CI->db->select('flag_mode')->where(array('setting_name' => 'maintenance'))->get('tbl_settings')->result();
//display view if true
if($check_maintenance[0]->flag_mode == '1')
$this->CI->load->view('vw_maintenance');
}
private function flag($command){
$this->CI->db->where('setting_name', 'evolving');
switch($command){
case "activate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 1) );
break;
case "deactivate":
$this->CI->db->update('tbl_settings', array('flag_mode' => 0) );
redirect(site_url('/'));
break;
}
}
}
$config['maintenance_mode'] = TRUE;
$config['maintenance_ips'] = array('0.0.0.0', '1.1.1.1', '2.2.2.2');
if(!in_array($_SERVER['REMOTE_ADDR'], $this->config->item('maintenance_ips')) && $this->config->item('maintenance_mode')) {
$route['default_controller'] = "your_controller/maintenance";
$route['(:any)'] = "your_controller/maintenance";";
}
<!DOCTYPE html>
<html>
<head>
<title>Maintenance</title>
</head>
<body>
<p>We apologize but our site is currently undergoing maintenance at this time.</p>
<p>Please check back later.</p>
</body>
</html>
维修
很抱歉,我们的网站目前正在进行维护
请稍后再查
我认为这很简单,只需调用构造函数上的视图,如下所示
评论(网站将是实时的)
取消注释(站点将在维护中)
您可以在CI应用程序的“视图”下使用自己的“maintenance.php”页面。我个人认为,最好是制作一个模块,将维护模式状态保存到数据库中。所以你可以从你的应用程序中动态地打开和关闭它。@tereško我是mvc的新手,我想确保我按照正确的方式设置这种功能。你的(安全的)live网站变得非常不安全:P。但这是OP要求的方式,so+1:)@Allendar你能解释一下这一改变是如何使网站变得不安全的吗?当加载维护视图后每个请求都消失时,人们是如何“仍然访问”它的?我已经尝试过实现这一点,当维护模式设置为TRUE时,我会得到一个空白页,尽管我的维护视图文件中有内容。如果删除“die();”命令,我会看到主页内容上方的文本,因此视图文件是正常的。为什么我只看到一个空白的白色页面?这是迄今为止最简单的解决方案,但是对于那些接收空白页面的人(像我一样),如果您将第3个参数设置为TRUE,您可以检索HTML,然后回显它$内容=$this->load->view('maintenance_view',,TRUE);echo$内容;这个整体解决方案没有问题,但不能在HTML之后发送标题。标题必须是您发送给用户的第一件事。谢谢Karman!这是我非常喜欢的解决方案,因为它可以在执行任何CI代码之前实现维护模式+1更多允许开发者访问IP。