Display site maintenence message using Codeigniter

Introduction

This example shows how we can display site maintenance message using Codeigniter 3 to the user. This tutorial will also teach you how to work with hooks in Codeigniter framework.

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.

Therefore, 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.

Final Result

Our final output will be something like

site under construction using codeigniter

Prerequisites

Codeigniter 3.1.10, PHP 7.0.15, Apache 2.4

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

Example with Source Code

We will go through the following steps to see how we display site maintenance message to the end users.

Enabling Hooks

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

$config['enable_hooks'] = TRUE;

Put a config variable in application/config/config.php file. This config variable will indicate whether site is in online or offline mode.

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

Defining Hooks

Define the hook in application/config/hooks.php file. I have put after each line what it does or indicates.

$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
);

Creating Hooks

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 */

In the above hooks file, inside the function is_offline(), we check whether the config variable is TRUE or FALSE and accordingly we will show the end users site maintenance message if the value is FALSE.

Source Code

download source code

That’s all. Thanks for reading.

1 thought on “Display site maintenence message using Codeigniter

Leave a Reply

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