I have a model:
<?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');
}
}
}
?>
The problem is when I visit this page @ localhost/mysite
it loads the following view : homepage.php
and this model (above) called homepagemodel.php
loads too, but a session is being created as soon as I load the page, how do I get it to be only if{} block
is executed ? If I put it inside the if{} block
, then a session will not be created in time using $this->load->library('session');
.
Anyone? I've been trying various fixes for this, but it keeps making duplicate rows with duplicate session values
Well, I am not sure if this is what you need, but here we go...
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');
}
}