我有这个在我的控制器命名的网页(我使用它作为一个终端分支到所有视图)会话数据CI3基本控制器
class pages extends MY_Controller {
public function verification(){
$session_data = $this->is_logged_in();
print_r($session_data);
if($this->is_logged_in()){
}
}
}
正如你所看到的,我有一个行“的print_r” session_data,我得到了一些结果,问题是它总是只返回'1'。
这是我的基础控制器上的功能。
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata();
return isset($user);
}
}
这是在我的身份验证器控制器上创建会话数据后登录的代码块。
Class user_authentication extends MY_Controller {
public function register(){
$this->form_validation->set_rules('username', 'Username','required|trim|min_length[8]|max_length[24]|is_unique[user.username]',
array('required' => 'You have not provided %s.','is_unique' => 'This %s already exists.'));
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]|max_length[16]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[6]|max_length[30]|is_unique[user.email]',
array('required' => 'You have not provided %s. ','is_unique' => 'This %s already exists.'));
$this->form_validation->set_rules('type', 'Account Type', 'trim|required|min_length[1]|max_length[1]',
array('required' => 'please select: 1 - normal, 2 - trainer'));
$this->form_validation->set_error_delimiters('', '');
if ($this->form_validation->run() == FALSE){
$this->load->view('templates/header');
$this->load->view('templates/navigation');
$this->load->view('pages/login');
$this->load->view('templates/footer');
}else{
$data = array('username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'),
'type' => $this->input->post('type'));
$result = $this->Account_model->create_account($data);
if($result == true){
$data['message_display'] = 'Registration Successful';
$this->load->view('templates/header');
$this->load->view('templates/navigation');
$this->load->view('pages/homepage',$data);
$this->load->view('templates/footer');
}else{
$data['message_display'] = 'Username and/or Email already exists';
$this->load->view('templates/header');
$this->load->view('templates/navigation');
$this->load->view('pages/login',$data);
$this->load->view('templates/footer');}
}
}
public function login(){
$this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[24]');
$this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[16]');
if($this->form_validation->run() == FALSE){
$this->load->view('templates/header');
$this->load->view('templates/navigation');
$this->load->view('pages/login');
$this->load->view('templates/footer');}
else{
$data = array('username' => $this->input->post('username'), 'password' => $this->input->post('password'));
$result = $this->Account_model->login_account($data);
$session_data = array('username' => $result['username'], 'email' => $result['email'], 'type' => $result['type']);
$this->session->set_userdata('isloggedin',$session_data);
$data['title'] = 'home';
$this->load->view('templates/header');
$this->load->view('templates/navigation');
$this->load->view('pages/home');
$this->load->view('templates/footer');}
}
现在有些东西基本上没用,但我把它们放在那里以备将来使用。比如$ data ['title']会使用它作为每个视图头部标题的占位符,因为我使用的是模板。
我做错了什么?为什么当我从MY_Controller打印会话数据时,它只返回'1'。
我一直在看导游,但其中大多数都过时了,我可能使用过去版本中的弃用东西,如果这样指出,我想练习编码整齐和干净。
对不起的答复超级晚。 你是什么意思设置另一个关键? 你的意思是在我的会话中设置另一个array_key?作为'is_logged_in'为1或0或真或假? –
为了给你一个准确的答案,你必须告诉我你的模型;) – sintakonte