Welcome Guest, Not a member yet? Register   Sign In
help creating a variable for logged in state that I can use in every controller and method site wide
#1

[eluser]Flying Fish[/eluser]
I want to create a boolean $logged_in variable that I can user throughout my whole site, particularly in my controller methods

So far, I'm extending the controller with MY_Controller and trying to define it there in the constructor function, but I'm getting a php error when trying to user $logged_in my controller methods, and not sure why

any help would be appreciated

Here's what MY_Controller looks like

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller {

    function MY_Controller()
    {
        parent::Controller();

        // Global Authentication Check
        if($this->authentication->check_login() === FALSE)
        {
            $logged_in = FALSE;
        }
        else
        {
            $logged_in = TRUE;
        }
        $this->load->vars('logged_in', $logged_in);

        // Template Chunks

            // Common
            $this->load->vars('head', $this->load->view('_inc/head', '', TRUE));
            $this->load->vars('masthead', $this->load->view('_inc/masthead', array('logged_in' => $logged_in), TRUE));
                // I had to pass login data to the masthead view in a vars array
                // or it could not see the $logged_in variable
                // see one of these forum threads for an explanation and alternative approaches
                // http://ellislab.com/forums/viewthread/111325/
                // http://ellislab.com/forums/viewthread/106648/
            $this->load->vars('footer', $this->load->view('_inc/footer', '', TRUE));
            $this->load->vars('breadcrumbs', $this->load->view('_inc/breadcrumbs', '', TRUE));
    
            // Admin
            $this->load->vars('admin_nav', $this->load->view('_inc/admin_nav', '', TRUE));
    }

}


/*
    End of file MY_Controller.php
    Location: /system/application/libraries/MY_Controller.php
*/


and here's where the error is coming from

Code:
class Account extends MY_Controller{

    function Account()
    {
        parent::MY_Controller();
                
        // Customize the tags that wrap arround the form validation error messages
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        // Load Model for Users
        $this->load->model('users');

    }
    
    function index()
    {
        // If the user is logged in, then they don't need to create an account send them to the /welcome/ page
        if($logged_in === FALSE)
        {
            redirect('/welcome/');
        }


Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: logged_in

Filename: controllers/account.php

Line Number: 27
#2

[eluser]Kamarg[/eluser]
You need to do $this->logged_in or it will go out of scope when the function you use it in returns.
#3

[eluser]Flying Fish[/eluser]
ok, would I do that in the controller method or when defining $logged_in
#4

[eluser]Kamarg[/eluser]
All the places you use $logged_in need to be replaced with $this->logged_in.
#5

[eluser]Flying Fish[/eluser]
I see, thanks




Theme © iAndrew 2016 - Forum software by © MyBB