Skip to content

Check Sessions with One Function

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

I have been toying around for weeks on the best solution on how to check for session data before allowing the user to access admin sections of their website.

Today I played around with creating a helper function that will do just that.

Helper File (session_helper.php) [code] <?php function check_sessions() { $ci=& get_instance(); if($ci->session->userdata('username')) { return true; } else { redirect('/'); } } ?> [/code]

Controller (admin_panel.php) [code] <?php Class Admin_panel extends Controller { function Admin_panel() { parent::Controller(); //check to make sure username is set in session check_sessions(); }

    function index() {
        $this->load->view('admin_panel_view');
    }
}

?> [/code]

Take note that I am auto loading my helper file in autoload.php

Hope this helps!

Clone this wiki locally