Welcome Guest, Not a member yet? Register   Sign In
Work with sessions in Codeigniter
#1

[eluser]Fyodor[/eluser]
Hello

I'd like to create authorization mechanism on my website but it it seems I am doing something wrong with sessions.
I assume I need to pass session data to index(), and after- to my view files.
However I can't do it.


Code:
//INDEX

public function index(){
    

  //load model
  $this ->load -> model('meetings_model');
  //prepare array before sending it to view
  $data=array('data' => $data, 'sessionstatus'=>$sessionstatus);
    

  $this->load->library('form_validation');
  $this->form_validation->set_error_delimiters('<span class="error">','</span>');
    
  //load view files through library DISPLAY
  //preheader, header, pagecontent and footer
  $this->display->show('00 index', $data);
  
} // end index

//part of LOGIN function
$result=$this->meetings_model->checkUserInDB($fieldInfo); //check if there is registered user in DB
if($result){
//echo "login success!";
$this->load->library('session');
$newdata = array(
               'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
$sessionstatus=$this->session->userdata['logged_in'];
redirect(base_url('meetings')); //I need to pass session's data to INDEX function
      

}
---------------------------------------------
I have one controller with "index" function and "login" function. I am not able to pass session data from "login" to index function:
Or may be I am wrong and there is another way to create authorization?

Any suggestions?

regards,
Fyodor
#2

[eluser]itz4mesays[/eluser]
Lemme just demonstrate with a good example

Model

public function verify($user, $password){

$sql = $this->db->get_where('user_login', $data);
if($sql ->num_rows() = =1){
// If there is a user, then create session data
$row = $sql->row();
$data = array(
'username' => $row->username,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
}


Controller
public function index(){
$this->load->library('session');
$user=array($this->input->post('username'));
$password=array($this->input->post('password'));
$q =$this->Your Model Name->verify($user, $password);
if($q){
echo 'Successful';
//load your view page, I named mine success.php
}
else{
echo 'Failed';
}
}


View (success.php)
&lt;?php
echo $this->session->userdata('username');
?&gt;

I hope it helps....
#3

[eluser]Fyodor[/eluser]
Hello, itz4mesays!
Thanks a lot, seems it works!

regards,
Fyodor




Theme © iAndrew 2016 - Forum software by © MyBB