0

我有一个模型:

<?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');.

任何人?我一直在为此尝试各种修复,但它会不断使用重复的会话值制作重复的行

4

1 回答 1

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');  
    }
}
于 2012-11-12T05:35:29.487 回答