Clain Dsilva Clain Dsilva - 1 year ago 71
PHP Question

How to create a common function in codeigniter for checking if session exist?

I am creating an application where in users have to login to access various modules. I need to check if the user session exist before providing access to each module.

Now I am checking session like this in each function / module / controller to avoid unauthorized access.

do something; }

Is there a better way to do this? can I have common function similar like


such that it can be called from any module / controller / function which is common to the whole project?

If so where should I write this common function such that it can be called from anywhere.

Answer Source

You want a helper function, here it is:

if ( ! function_exists('sessionExist'))
    function sessionExist(){
        $CI =& get_instance();
        return (bool) $CI->session->userdata('userId');

Save the file in application/helpers/ and include it your application/config/autoload.php file:

$autoload['helper'] = array('my_helper_file');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download