我在会话的Codeigniter中遇到一些问题

时间:2018-11-22 22:53:09

标签: php codeigniter session

我尝试检查登录他/她的帐户的人是否显示带有会话的页面,我在库中加载了会话,但是该会话无法正常工作 对不起我的英语不好 感谢您的帮助:)

这是模型

class login_model extends CI_Model
{

function __construct()
{
    parent::__construct();
}

public function check()
{
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    $remember = $this->input->post('remember');

    $this->db->select('username','password');
    $this->db->from('user');
    $this->db->where('username',$username);
    $this->db->where('password',md5($password));

    $result = $this->db->count_all_results();

    if($result > 0)
    {
        $data_session = array(
            'username'=>$username,
            'islogin'=>true,    
        );
        $this->session->set_userdata($data_session);
        redirect('post/index');
    }

这是控制者

class Post extends CI_Controller
{
public function index()
{

    $login = $this->session->userdata('islogin');

    if($login == true)
    {
        $data = array (
            'name' => 'Hello',
        );
        $this->load->view('post/index',$data);
    }
    else {
        redirect('login/index');
    }

}
}

这是配置

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'weblogdata';
$config['sess_expiration'] = 3600;
$config['sess_save_path'] = 'tbl_ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;

$config['encryption_key'] = m.D~wt,wA:MjS9$]g<H4Z7eW.7`0vDbX$F`LUgCg+>$1?0L$vq1:7vaVf&d{U(   

2 个答案:

答案 0 :(得分:0)

在contruct()上设置会话条件,将控制器更改为:

function __construct() {
parent::__construct();
        if($this->session->userdata('islogin') != true)
        {
            redirect('login/index');
        }
}

function index() {
        $data = array (
        'name' => 'Hello',
    );
    $this->load->view('post/index',$data);
}

答案 1 :(得分:0)

将您的代码更改为此

class Post extends CI_Controller
{
public function index()
{

    $login = $this->session->userdata('islogin'); // $login will be 1 

    if($login)
    {
        $data = array (
            'name' => 'Hello',
        );
        $this->load->view('post/index',$data);
    }
    else {
        redirect('login/index');
    }

}
}