EllisLab text mark
Advanced Search
     
Global Site (Site control)
Posted: 22 September 2007 09:22 AM   [ Ignore ]
Joined: 2007-01-04
8 posts

I thought I would share a very simply code I have written to control a whole site from a config file, I call this simple script global site, this allows you to disable a whole site or just one or many controllers so they display a site is currently down message.

It also has a feature where you can allow ip addresses to view the site even if it is currently disabled for the rest of the world.

I find this script has helped me for any development site I have been creating so only a customer can view the progress and not the rest of the world or if you are doing a major update it is great for testing to make sure a cutover has gone through smoootly while informing others it is currently being updated.

The Library

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

class 
GlobalSite 
{
    
function __construct() {
        $this
->CI=& get_instance();

        
log_message('debug'"Global Class Initialized");
        
$this->ip_address $this->CI->input->ip_address();
    
}
    
function resetTrue() {
        
if($this->CI->config->item('disable_site') == TRUE)
        
{
            
// Check if the ip address is allowed on the site while it is down.
            
foreach($this->CI->config->item('allow_ips') as $key => $value)
            
{
                
// Allow the ipaddresses listed in the config file
                
if($value === $this->ip_address)
                
{
                    $this
->CI->config->set_item('disable_site'false);
                
}    
            }
        }
    }
    
function resetSpecificTrue() {
        
if($this->CI->config->item('disable_'.$this->CI->uri->segment(1)) == TRUE)
        
{
            
// Check if the ip address is allowed on the site while it is down.
            
foreach($this->CI->config->item('allow_ips') as $key => $value)
            
{
                
// Allow the ipaddresses listed in the config file
                
if($value === $this->ip_address)
                
{
                    $this
->CI->config->set_item('disable_'.$this->CI->uri->segment(1), false);
                
}    
            }
        }
    }

Global Config

$config['disabled_message'"The site is currently down for upgrades, please return in the near future.";
///////////////////////////////////
// Custom disables               //
// Disables the whole site when  //
// set to true                   //
///////////////////////////////////

$config['disable_site'FALSE;

///////////////////////////////////
// Disables a specific controller//
// disable_site overrides this     //
// call.             //
///////////////////////////////////

$config['disable_welcome'FALSE;

// Can be as many of these as you want

/////////////////////////////////////
// Sets the message for a disabled //
// controller                      //
/////////////////////////////////////

$config['disabled_specific'"This section is currently disabled, please return later";

/****************************************************************/
/* ip addresse allowed to view the site even if it is disabled  */
/****************************************************************/

$config['allow_ips'= array('localhost' => '127.0.0.1',
                 
'work' => '192.168.6.16',
                 
'home' => '111.111.11.11'); 

Inside you constructor

class Whatever extends Controller {
      
function Whatever() {
        parent
::Controller();
        
        
$this->globalsite->resetTrue(); // if set to true disbales the whole site except for the allowed ip addresses
        
$this->globalsite->resetSpecificTrue();  // if $config['disable_whatever'] is set to true it will disable only that controller except for allowed ip addresses
       
}

The View

<?php if($this->config->item('disable_site')):?>
<?php $this
->load->view($this->config->item('theme').'templates/content_disabled')?>
<?php 
else:?>
    <?php 
if($this->config->item('disable_'.$this->uri->segment(1))):?>
    <?php $this
->load->view($this->config->item('theme').'templates/content_disabled')?>
    <?php 
else:?>
    <?php $this
->load->view($this->config->item('theme').'templates/content')?>
    <?php 
endif;?>
<?php 
endif;?> 

The view is best set through a container to load content or disabled content files.

I find this is best set through the autoload but can also be activated in a controller construct as well.

Hope this is helpful to someone else.

 
Posted: 22 September 2007 09:19 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-20
346 posts

nice idea will come in handy im sure.

 Signature 

Twitter

 
Posted: 23 September 2007 01:26 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-07
690 posts

that’s a good idea, thanks

 Signature 

jtaby.com

 
Posted: 23 September 2007 10:53 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2007-08-05
251 posts

This is great, thanks!

Just for reference though, how does the script know to pull the global config or are you adding that code to the config.php file?

Thanks.

 Signature 

CI Base - CodeIgniter Repository

 
Posted: 23 September 2007 12:47 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2006-10-03
31 posts

lol build a library when you can edit the index.php file where the site starts from ...
you can put in front ... an array with some ips and then put the condition

$allowed_ips=array('ip1''ip2'); //etc
 
if (!in_array($allowed_ips)) do die() or something } 

i wrote two lines of code… u wrote ... many smile

 Signature 

Stupid people do stupid things, smart people outsmart each otherSOAD

 
Posted: 24 September 2007 12:53 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2007-01-04
8 posts
Kemik - 23 September 2007 02:53 PM

This is great, thanks!

Just for reference though, how does the script know to pull the global config or are you adding that code to the config.php file?

Thanks.

I just call this in the autoload.php because it needs to be called for each controller that loads

 
Posted: 24 September 2007 12:57 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2007-01-04
8 posts
Skulls - 23 September 2007 04:47 PM

lol build a library when you can edit the index.php file where the site starts from ...
you can put in front ... an array with some ips and then put the condition

$allowed_ips=array('ip1''ip2'); //etc
 
if (!in_array($allowed_ips)) do die() or something } 

i wrote two lines of code… u wrote ... many smile

Yes that may be true and comment accepted, but this also checks if each controller is active and I may not want to disable the whole site, it aslo loads it directly into the current template structure of the page and did not want to call a die with just an error message by itself.

I am sure there is probably a better way of doing this but I wanted to have a lot more control over each seperated controller than the whole site