Taylor
Taylor

Reputation: 3141

Codeigniter - Put site in Maintenance mode WITHOUT using htaccess

I have finished building my site using codeigniter and have also built an admin panel where there is an option to put the site offline.

It is saved in the database as an boolean value so if it's 1 the site is on or else it's offline. I have also finished making a offline.php view file.

I have also made a whitelisted_ip column in my database where admin IP's are saved in an array. The only problem is displaying the maintenance page to everyone but the whitelisted IP's.

I can't use htaccess for this because I am controlling from admin panel and database.

What I want:

What I have so far:

I just need to be able to have every single page on my site have this code or tell every single page on my site that if the site is offline, show that offline page.

Any help is highly appreciated. Thanks

Edit: Please let me know if this is going to work (works for me).

MY_Controller.php file:

class MY_Controller extends CI_Controller
{
public function __construct()
{
    parent::__construct();
}

function offline_mode()
{

    $this->db->select('*');
    $this->db->from('site');
    $this->db->where('1', 1);
    $query = $this->db->get();
    $result = $query->row();

    $whitelisted_ip = explode(",", $result->whitelisted_ip);
    $ip = $this->input->ip_address();


    if ($result->site_offline) {
        if (!in_array($ip, $whitelisted_ip)) {

            $this->load->view('site_offline');
        }
    } else {
        redirect(base_url());
    }

} }

All my other controllers:

class Page extends MY_Controller
{
public function __construct()
{
    parent::__construct();
    $this->offline_mode();
    $this->load->helper('url');
}

Is this a good way to go?

Upvotes: 1

Views: 3619

Answers (1)

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

change like this.

public function __construct()
{
    parent::__construct();
    $this->db->select('*');
    $this->db->from('site');
    $this->db->where('1', 1);
    $query = $this->db->get();
    $result = $query->row();

    $whitelisted_ip = explode(",", $result->whitelisted_ip);
    $ip = $this->input->ip_address();


    if ($result->site_offline) {
        if (!in_array($ip, $whitelisted_ip)) {

            $this->load->view('site_offline');
        }
    } else {
        redirect(base_url());
    }

    } 
}

what i mean is, no need to keep function and call that every time. keep the code in constructor. so every time it checks if the site is offline or not.

If at all you want to keep is as function, alter constructor like this,

public function __construct()
{
    parent::__construct();
    $this->offline_mode();
}

So in both cases, u ll write code to check only once, no need to call the function in each controller.

Upvotes: 1

Related Questions