在codeigniter中重复会话

时间:2012-11-11 20:57:21

标签: php codeigniter session session-state

我有一个模特:

<?php

class Loginfunctionmodel extends CI_Model{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();

    }

    # check_if_loggedin() below checks if user has session w/ loggedIn=1, then displays needed supernav elements
    function check_if_loggedin(){
        $this->load->library('session');
        # user has loggedin Val = 1 and valid session_id
        $loggedInSetSuccess = 1;
        $sess = $this->session->userdata('session_id');
        $sessionsDbCompare = $this->db->get_where('Client',array('session_id'=>$sess,'loggedIn'=>$loggedInSetSuccess));
        // no session id for user in database... load login
        if($sessionsDbCompare->num_rows() == 1 ){
                $this->load->view('supernavigationloggedin.php');
                $this->load->view('primarynavigation.php');        
        }
        // otherwise problem with session count for the user is greater than 1.
        // Regenerate values
        elseif($sessionsDbCompare->num_rows() > 1){
            #remove current errorneous [session_id] data
                $this->session->unset_userdata();
            #reset [session_id] data
                $this->session->set_userdata();
            # load logged in views, given single session id exists.
                $this->load->view('supernavigationnotloggedin.php');
                $this->load->view('primarynavigation.php');               
        }

        // otherwise problem with session or loggedIn ... show login form.
        else{
            #remove current errorneous [session_id] data; load not logged in views.
            $this->session->unset_userdata();
            $this->load->view('supernavigationnotloggedin.php');
            $this->load->view('primarynavigation.php');     
        }

    }

}
?>

问题是当我访问此页面@ localhost/mysite时,它会加载以下视图:homepage.php并且此模型(上面)也会加载homepagemodel.php,但会话正在创建为加载页面后,如何才能使if{} block被执行?如果我将放在 if{} block内,则不会使用$this->load->library('session');及时创建会话。

任何?我一直在尝试各种修复,但它不断创建具有重复会话值的重复行

1 个答案:

答案 0 :(得分:0)

嗯,我不确定这是否是你需要的,但我们走了......

function check_if_loggedin()
{        
    $is_logged = $this->session->userdata('loggedIn');
    $sess = $this->session->userdata('session_id');
    $sessionsDbCompare = $this->db->get_where('Client',array('session_id'=>$sess));

    if ($is_logged == 1 && $sessionsDbCompare->num_rows() == 1)
    {
        $this->session->set_userdata('loggedIn', 1);
        $this->load->view('supernavigationloggedin.php');
        $this->load->view('primarynavigation.php');
    }
    else
    {
        $this->session->unset_userdata();
        $this->load->view('supernavigationnotloggedin.php');
        $this->load->view('primarynavigation.php');  
    }
}