How to build in 'maintenance mode' in Codeigniter?
Here my solution, works fine for me:
The below will call maintanance.php immediately, so you can go ahead and break your CI code without the world seeing it.
Also allow you to add you own ip address so that you can still access the site for testing etc.
In index.php add at top:
$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();
}
}
Add file Maintanance.php in same folder as index.php (or update path above):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Maintenance</title>
<style>
body {
width:500px;
margin:0 auto;
text-align: center;
color:blue;
}
</style>
</head>
<body>
<img src="images/home_page_logo.png">
<h1><p>Sorry for the inconvenience while we are upgrading. </p>
<p>Please revisit shortly</p>
</h1>
<div></div>
<img src="images/under-maintenance.gif" >
</body>
</html>
<?php
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600');
?>
Here is what I've come up with for creating a maintenance mode.
- Enable Hooks in the config.php file
- Create an error_maintenance.php page under errors folder
- Create a Hook called maintenance
- In the hooks config setup your hooks call to run on post_controller
application/errors/error_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>
application/hooks/maintenance.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class maintenance
{
var $CI;
public function maintenance()
{
$this->CI =& get_instance();
$this->CI->load->config("config_maintenance");
if(config_item("maintenance"))
{
$_error =& load_class('Exceptions', 'core');
echo $_error->show_error("", "", 'error_maintenance', 200);
exit;
}
}
}
application/config/hooks.php
$hook['post_controller'][] = array(
'class' => 'maintenance',
'function' => 'maintenance',
'filename' => 'maintenance.php',
'filepath' => 'hooks',
'params' => array()
);
Here my solution, simply, clean and effectively for all urls calls and SEO respects:
Add this variables in: application/config/config.php
$config['maintenance_mode'] = TRUE;
$config['maintenance_ips'] = array('0.0.0.0', '1.1.1.1', '2.2.2.2');
Add this conditional at the end of: application/config/routes.php
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";";
}
Create method maintenance in: application/controllers/your_controller.php
function maintenance() {
$this->output->set_status_header('503');
$this->load->view('maintenance_view');
}
Create view: application/views/maintenance_view.php
<!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>
Extend the CI_Controller by putting a new file in your core directory called MY_Controller.
In this file's constructor, do something like this:
public function __construct()
{
parent::__construct();
if($this->config->item('maintenance_mode') == TRUE) {
$this->load->view('maintenance_view');
die();
}
}
Let all controllers in your app inherit from that class.