我正在尝试学习Codeigniter,我有以下代码,它给了我“页面没有正确重定向...”错误。
public function user_login_process() {
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$classes['class'] = $this->show_classes();
$this->load->view('header');
if ($this->form_validation->run() == FALSE) {
if (isset($this->session->userdata['logged_in'])) {
$this->load->view('admin_page', $classes);
} else {
$this->load->view('login_form');
}
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if ($result == TRUE) {
$username = $this->input->post('username');
$result = $this->login_database->read_user_information($username);
if ($result != false) {
$session_data = array(
'username' => $result[0]->user_name,
'email' => $result[0]->user_email,
);
$this->session->set_userdata('logged_in', $session_data);
$this->load->view('admin_page', $classes);
}
} else {
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('login_form', $data);
}
}
}
如果我注释掉这一行
$this->load->view('header');
页面正确加载,没有标题。但每当我尝试加载标题时,它都会失败。问题不在于我相信它在其他页面上正确加载的标题。
某些上下文:我正在尝试创建一个管理面板,有一个登录页面,一个注册页面和一个管理页面。此代码段在成功登录后处理管理页面的加载。登录和注册页面都正确加载头文件。
我查看了一些相关问题,但似乎无法找到解决此问题的正确方法,因为我不知道实际问题是什么。 任何帮助表示赞赏。
答案 0 :(得分:1)
在您的代码中,未加载重定向和视图文件加载。下面我根据需要在我的评论中写了正确的代码。
if (isset($this->session->userdata['logged_in'])) {
redirect(base_url('YOUR_AUTHORIZED_CONTROLLER_OR_FUNCTION'));
}
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$classes['class'] = $this->show_classes();
$this->load->view('header');
if ($this->form_validation->run() == FALSE) {
// $this->session->userdata['logged_in'] this should not be used here. Instead you should validate your controller or methods for authorized area for logged in users
$this->load->view('login_form');
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if ($result == TRUE) {
$username = $this->input->post('username');
$result = $this->login_database->read_user_information($username);
if ($result != false) {
$session_data = array(
'username' => $result[0]->user_name,
'email' => $result[0]->user_email,
);
$this->session->set_userdata('logged_in', $session_data);
// Commenting below line as you should redirect to your default authorised URL
//$this->load->view('admin_page', $classes);
$this->session->set_flashdata('success', 'Great! You have successfully logged in.');
redirect(base_url('YOUR_AUTHORIZED_DEFAULT_CONTROLLER_OR_FUNCTION'));
}
} else {
$this->session->set_flashdata('error', 'Invalid Username or Password');
// Redirect to login method and controller
redirect(base_url('LOGIN_ACTION'));
}
}
查看:强> 在您的视图中使用下面的行显示错误/成功消息。最好创建新的视图文件以显示其中的所有消息,并在加载标题视图后加载此视图:
<?php echo $this->session->flashdata('error'); ?>
<?php echo $this->session->flashdata('success'); ?>
如果您需要任何澄清,请告诉我。