Skip to content

I want to take my site offline for maintenance rogierb

World Wide Web Server edited this page Jul 4, 2012 · 15 revisions

This is my first attempt to creat a WIKI page so bare with me.

[h2]Overview[/h2]

This approach relies on the build-in hook system.

[h2]Approach[/h2]

[h4]Step one[/h4] 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: [code] $config['enable_hooks'] = TRUE; [/code] You can find the file in the application/config folder

[h4]Step two[/h4] 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: [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; [/code]

[h4]Step three[/h4] 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 [code] $hook['pre_system'][] = array( 'class' => 'site_offline_hook', 'function' => 'is_offline', 'filename' => 'site_offline_hook.php', 'filepath' => 'hooks' ); [/code]

[h4]Step four[/h4] 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/hookssite_offline_hook.php

[code] <?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 '&lt;html&gt;&lt;body>Due to maintenance this site is offline.&lt;/body&gt;&lt;/html>';
}

} /* Location: ./system/application/hooks/site_offline_hook.php */ [/code]

[h4]Step five - the final step[/h4] You can set you site offline by setting $config['is_offline'] = TRUE;

[h5]Customize the message[/h5]

Clone this wiki locally