Welcome Guest, Not a member yet? Register   Sign In
Where to put a "global variable" for the entire website
#12

[eluser]simshaun[/eluser]
What I'm building is a Users lib that spans across both the frontend and backend.

That class was in no way, shape, or form, complete by any means. It was merely an example of what I'm doing that allows myself the use of my models in combination with sessions to set vars in the config.

In the end I plan on doing something like what you posted above xwero, except a bit different.

The get_user_level method must be called on each page load for what I'm building.
If a user is demoted in any way, it must be nearly instantaneous, otherwise the user can still do things freely until the next time he logs in.

Here's the general concept of what I'm gonna do.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Class is auto-loaded by CI.
*/
class Auth {

    var $CI;

    function Auth()
    {
        $this->init();
    }

    /**
    * Called by construct.
    *
    */
    function init()
    {
        $this->_get_ci();
        $this->CI->load->model('UsersModel');

        if ($this->is_logged_in()){
            $user_level    = $this->CI->UsersModel->get_user_level($this->CI->session->userdata('user_id'));
            $this->CI->config->set_item('cur_user_level', $user_level);
        }
    }

    /**
    * Returns TRUE/FALSE if user is logged in.
    *
    */
    function is_logged_in()
    {
        if ($this->CI->session->userdata('user_id') !== FALSE && ctype_digit($this->CI->session->userdata('user_id'))){
            return TRUE;
        }

        ## TODO: Implement cookie "remember-me" support via a token.

        return FALSE;
    }

    /**
    * Called upon login.
    *
    */
    function do_login()
    {
        // sample data for now
        $sess_data    = array(
            'user_id'        => 1
            ,'username'        => 'jdoe'
            ,'user_fname'    => 'John'
            ,'user_tz'        => 'UM5'
        );

        $this->CI->session->set_userdata($sess_data);
    }

    function _get_ci()
    {
        $this->CI =& get_instance();
    }
}

// end of file


Messages In This Thread
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 08:08 PM
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 09:21 PM
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 09:24 PM
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 09:28 PM
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 09:39 PM
Where to put a "global variable" for the entire website - by El Forum - 12-29-2008, 07:37 PM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 02:31 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 02:51 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 03:07 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 04:32 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 05:04 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 09:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB