Display site maintenence message using Codeigniter

This example shows how we can show site maintenance message to the user. We build the website and live our website but it does not mean that all worked have been done for the website and in future we will never update or modify our site. So even after the website goes live we need to update the site continuously with new posts, news, products etc. Hence we need to halt the site so that end users cannot access the site for some time until or unless we are done with our updates. So when we modify or update on our site then it is not a good idea to allow end users access our site because it may cause problem at our end while updating as well as at end users end while they are accessing. It’s not also good idea halting the site without any appropriate message to the end users when we halt our site for few minutes or hours because they won’t know what’s happening to the site. So for this reason we need to show a friendly message to the end-users so that they can understand that the site is under maintenance and once the maintenance works are over then the site will be available again for access.

Our final output will be something like

site under construction using codeigniter

So I will use here hook. For more information on hooks please go through the Codeigniter Documentation http://ellislab.com/codeigniter/user-guide/general/hooks.html

First enable the hook in application/config/config.php file

$config['enable_hooks'] = TRUE;

Put a config variable in application/config/config.php file

$config['is_offline'] = FALSE; // make it TRUE when you want your site to be offline

Define the hook in application/config/hooks.php file

$hook['pre_system'][] = array(
'class' => 'site_offline', // name of the class - site_offline
'function' => 'is_offline', // function which will be executed in the class - site_offline
'filename' => 'site_offline.php', // filename for the class - site_offline
'filepath' => 'hooks' // filepath - where the classfile resides
);

Now create the class file – site_offline.php and put it under application/hooks directory

<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

/**
* Description of site_offline
*
* @author admin
*/
class Site_Offline {

function __construct() {

}

public function is_offline() {
if (file_exists(APPPATH . 'config/config.php')) {
include(APPPATH . 'config/config.php');

if (isset($config['is_offline']) && $config['is_offline'] === TRUE) {
$this->show_site_offline();
exit;
}
}
}

private function show_site_offline() {
echo '<html><body><span style="color:red;"><strong>The site is offline due to maintenance. We will be back soon. Please check back later</strong></span>.</body></html>';
}

}

/* End of file site_offline.php */
/* Location: ./application/hooks/site_offline.php */

 

That’s all. Thanks for your reading. Please do not forget to leave a comment.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials Twitter Facebook  Google Plus Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *