我正在尝试在Codeigniter中创建一个会话。我在自动加载中加载了session
和encrypt
库。默认情况下,会打开login.php
页面。如果用户已经登录,我想打开dashboard.php
页面。我编写了以下代码,但即使在登录后也会打开login.php页面。
请告诉我如何检查会话是否已设置。
public function index() { $data['products_data']= $this->products_model->all_products(); // $this->load->view('login'); $this->form_validation->set_rules('username','Username','required'); $this->form_validation->set_rules('password','Password','required'); if ($this->form_validation->run()== false) { $this->load->view('login'); } else { if ($this->session->userdata('login_status')) { $this->load->view('header'); $this->load->view('dashboard'); $this->load->view('footer'); } else { $username= $this->input->post('username'); $password= $this->input->post('password'); if( $this->products_model->login_model($username,$password)) { $sess_data= array( 'username'=> $username, 'login_status'=>'1' ); $session_data = $this->session->set_userdata($sess_data); $this->load->view('header'); $this->load->view('dashboard'); $this->load->view('footer'); } } } }
答案 0 :(得分:2)
$sessdata = $this->session->set_userdata($session_data);
if (isset($sessdata))
{
//session exists
}
else{
// no session value
}
因此,您的控制器应如下所示,以避免在页面刷新时重定向到登录页面
function index() {
if ($this->session->userdata('login_status')) {
//write your code for redirect to dashboard
} else {
$data['products_data'] = $this->products_model->all_products();
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('login');
} else {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->products_model->login_model($username, $password)) {
$sess_data = array(
'username' => $username,
'login_status' => '1'
);
$session_data = $this->session->set_userdata($sess_data);
//write your code for redirect to dashboard
}
}
}
}
答案 1 :(得分:1)
我更喜欢为仪表板创建另一个控制器,并在成功登录后将其重定向到那里。它很干净,你可以轻松做进一步的改变
if( $this->products_model->login_model($username,$password))
{
$sess_data= array(
'username'=> $username
);
$session_data = $this->session->set_userdata($sess_data);
if(isset($session_data))
redirect(site_url('/dashboard/'));
}
FM:$ sess_data不应该是单引号
答案 2 :(得分:1)
我将使用会话记录登录状态。
$session_data= array(
'username'=> $username,
'lgoin_status' => 1
);
$this->session->set_userdata('$session_data');
我会检查会话的login_status
if ($this->session->userdata('login_status')) {
// do something
}
修改强>
在您的代码中,您应该首先检查会话,所以..
public function index()
{
if ($this->session->userdata('login_status'))
{
// do something
}
else
{
$data['products_data']= $this->products_model->all_products();
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run()== false)
{
$this->load->view('login');
}
...
}
答案 3 :(得分:1)
删除
上的单引号 $this->session->set_userdata('$sess_data');
它将变量$sess_data
视为常规字符串。
必须是:
$this->session->set_userdata($sess_data);