Skip to content

I want to take my site offline for maintenance rogierb

Derek Jones edited this page Jul 5, 2012 · 15 revisions

Category:Approaches::I want to take my site offline for maintenance

Overview

This approach relies on the build-in hook system.

Approach

Step one

First of all we need to enable hooks. If you have already done so, skip to the next step

You have to edit the config.php file to enable hooks. Set the $config['enable_hooks'] to TRUE like so:

$config['enable_hooks'] = TRUE;

You can find the file in the application/config folder

Step two

In the same file a new config item is introduced: $config['is_offline'] At the bottom ( just before the 'end of config.php') insert the following code:

/*
|--------------------------------------------------------------------------
| Maintenance site
|--------------------------------------------------------------------------
|
| For whatever reason sometimes a site needs to be taken offline.
| Set $config['is_offline'] to TRUE if the site has to be offline
|
| $config['is_offline'] = TRUE; // site is offline
| $config['is_offline'] = FALSE; // site is online
*/

 $config['is_offline'] = FALSE;

Step three

To let the system know what hook to use we need to edit hooks.php. This is in the application/config folder aswell.

You need to insert the follwing code

$hook['pre_system'][] = array(
     'class'    => 'site_offline_hook',
     'function' => 'is_offline',
     'filename' => 'site_offline_hook.php',
     'filepath' => 'hooks'
     );

Step four

Almost there. We need to create a new hook-file.

Create a new file called site_offline_hook.php in the application/hooks folder. If there is no such folder, then create it.

Insert the following code into application/hooks/site_offline_hook.php

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

/**
 * Check whether the site is offline or not.
 *
 */
class site_offline_hook {

    public function __construct()
    {
    log_message('debug','Accessing site_offline hook!');
    }
    
    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>Due to maintenance this site is offline.</body></html>';
    }

}
/* Location: ./system/application/hooks/site_offline_hook.php */

Step five - the final step

You can set you site offline by setting $config['is_offline'] = TRUE; To put the site online again, change it back to: $config['is_offline'] = FALSE;

Customize the maintenance message

Just edit the echo statement in the function show_site_offline(). This function is located in application/hooks/site_offline_hook.php

Note to PHP4 users This is build for PHP5, if you want to use this, then rename the contructor to "site_offline_hook". That should be all, but this is untested!

Clone this wiki locally