• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where to put a "global variable" for the entire website

#11
[eluser]xwero[/eluser]
The idea in this thread is to have a 'global' variable. simshaun's variable is connected to a user and so it's not a 'global' variable. That is the first mistake.

I don't think the user_level triggers a redirect to the login in form that is where is the user_id for. A user_level flag is most of the time to show/hide page content.

The only time i think the user_level is useful as a global variable is when the content of the pages can be edited on the pages themselves but most of the time you have an admin section to take care of the input because much data is displayed in various forms.

#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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.