How to use global variable in PHP Codeigniter

user385729

I have implemented the login logic in an MVC application; I want to see if the user has filled the username and passowrd incorrectly and if so I want to show a notifictaion in the view; so I'm passing this information via $data['er']; but for some reason it is not catching this data:

Please let me know if my question is clear or not; and if any clarification is needed, please let me know which part is ambiguous

My code:

class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();
         $GLOBALS['er'] = False;
    }



    public function index() {

        $data['er']=$GLOBALS['er'];
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);
    }

    public function validate_credentials() {

        $this->load->model('user_model');
        $query = $this->user_model->validate();
        if ($query) {
            $data = array(
                'username' => $this->input->post('username'),
            );
            $this->session->set_userdata($data);
            redirect('project/members_area');
        } else {
            $GLOBALS['er'] = TRUE;
            $this->index();

        }
    }

} 
doitlikejustin

Don't use GLOBALS you can just use a private variable in your class.

  • Create the variable above your __construct function like private $er
  • In your __contruct function set the default value
  • Set and get in your public function using $this->er

Implemented in your code:

class Login extends CI_Controller {

    private $er;

    public function __construct() {
        parent::__construct();
        $this->er = FALSE;
    }

    public function index() {
        $data['er']= $this->er;
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);
    }

    public function validate_credentials() {
        $this->load->model('user_model');
        $query = $this->user_model->validate();
        if ($query) {
            $data = array(
                'username' => $this->input->post('username'),
            );
            $this->session->set_userdata($data);
            redirect('pmpBulletin/members_area');
            //die(here);
        } else {
            $this->er = TRUE;
            $this->index();
        }
    }
} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to define Global variable in codeigniter?

From Dev

html/javascript/php: how do i use a "global variable"?

From Dev

How to define a global variable(value) in codeIgniter

From Dev

How to declare a variable for global use

From Dev

How to use Global Variable in Python

From Dev

How to use the value of a global variable?

From Dev

How to unset global variable in php?

From Dev

Codeigniter global variable in view

From Dev

Having a PHP function use a global variable as a default?

From Dev

Having a PHP function use a global variable as a default?

From Dev

How to create and use a global variable on Mule config?

From Dev

How to use AsyncTask to update a global variable

From Dev

How to define a global variable in LLVM and use it in C?

From Dev

How to use global variable in symfony twig file?

From Dev

How to create and use a global variable on Mule config?

From Dev

How to use AsyncTask to update a global variable

From Dev

How to use sharedInstance on swift as global variable?

From Dev

How to use the UIStepper to control global variable?

From Dev

How to create a global variable and use it in other pages?

From Dev

how to use a global variable inside a function in javascript?

From Dev

How to use global variable in onClick() method?

From Dev

How to use a class attribute instead of a global variable?

From Dev

How to use the global variable to initialize the class property?

From Dev

How to use a datatrigger that needs a static global variable?

From Dev

CodeIgniter counter using "global" variable

From Dev

How to Create Global Variable Dynamically in PHP

From Dev

How to share global variable across thread in php?

From Dev

How do declare global variable in PHP class

From Dev

How to assign value to global variable in PHP

Related Related

  1. 1

    how to define Global variable in codeigniter?

  2. 2

    html/javascript/php: how do i use a "global variable"?

  3. 3

    How to define a global variable(value) in codeIgniter

  4. 4

    How to declare a variable for global use

  5. 5

    How to use Global Variable in Python

  6. 6

    How to use the value of a global variable?

  7. 7

    How to unset global variable in php?

  8. 8

    Codeigniter global variable in view

  9. 9

    Having a PHP function use a global variable as a default?

  10. 10

    Having a PHP function use a global variable as a default?

  11. 11

    How to create and use a global variable on Mule config?

  12. 12

    How to use AsyncTask to update a global variable

  13. 13

    How to define a global variable in LLVM and use it in C?

  14. 14

    How to use global variable in symfony twig file?

  15. 15

    How to create and use a global variable on Mule config?

  16. 16

    How to use AsyncTask to update a global variable

  17. 17

    How to use sharedInstance on swift as global variable?

  18. 18

    How to use the UIStepper to control global variable?

  19. 19

    How to create a global variable and use it in other pages?

  20. 20

    how to use a global variable inside a function in javascript?

  21. 21

    How to use global variable in onClick() method?

  22. 22

    How to use a class attribute instead of a global variable?

  23. 23

    How to use the global variable to initialize the class property?

  24. 24

    How to use a datatrigger that needs a static global variable?

  25. 25

    CodeIgniter counter using "global" variable

  26. 26

    How to Create Global Variable Dynamically in PHP

  27. 27

    How to share global variable across thread in php?

  28. 28

    How do declare global variable in PHP class

  29. 29

    How to assign value to global variable in PHP

HotTag

Archive