如何在PHP Codeigniter中使用全局变量
我已经在MVC应用程序中实现了登录逻辑;我想看看用户是否错误地填写了用户名和密码,如果是这样,我想在视图中显示一个通知;所以我通过$data['er']传递这个信息;但出于某种原因,它无法捕获这些数据: 请让我知道我的问题是否清楚;如果需要任何澄清,请让我知道哪部分不明确 我的代码:如何在PHP Codeigniter中使用全局变量,php,codeigniter,Php,Codeigniter,我已经在MVC应用程序中实现了登录逻辑;我想看看用户是否错误地填写了用户名和密码,如果是这样,我想在视图中显示一个通知;所以我通过$data['er']传递这个信息;但出于某种原因,它无法捕获这些数据: 请让我知道我的问题是否清楚;如果需要任何澄清,请让我知道哪部分不明确 我的代码: class Login extends CI_Controller { public function __construct() { parent::__construct();
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();
}
}
}
不要使用
GLOBALS
您可以在类中使用私有变量
- 在您的
函数上方创建变量,如\u构造
private$er
- 在
函数中设置默认值
- 使用
$this->er
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();
}
}
}