0
我想在登錄後訪問會話變量,但沒有在會話變量中顯示,但登錄和註銷功能運行良好,我也在模型部分中使用了*(全選)。 控制器部分 無法在CodeIgniter中調用會話變量
<?php
class login extends CI_Controller
{
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('session');
}
function index()
{
$data['main_content'] = 'index';
$this->load->view('includes/template', $data);
}
function login_form(){
if($this->session->userdata('logged_in')==1)
{
$this->index();
}
else
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
}
function validate_credentials()
{
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
if($this->form_validation->run())
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query)
{
//$data = array('email'=>$this->input->post('email'),
//'is_logged_in' => true);
$this->load->helper('url');
$this->load->library('session');
$this->session->set_userdata(
array(
'id' =>$user_details->id,
'email' =>$user_details->email,
'name' =>$user_details->name,
'logged_in'=>1));
redirect('login/index');
}
else
{
$this->session->set_flashdata('login_failed', 'invalid username/password.');
$this->login_form();
}
}
else
{
$this->login_form();
}
}
function logout(){
$this->session->sess_destroy();
$this->index();
}
}
?>
模型部分
<?php
class membership_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
function validate($data)
{
$this->db->select('*');
$this->db->from('register');
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', $this->input->post('password'));
$query= $this->db->get();
if($query->num_rows == 1)
{
return true;
}
else
{
return false;
}
}
}
?>