Welcome Guest, Not a member yet? Register   Sign In
How do you check the session? My code is wrong help.
#1

[eluser]liesbetweenyoureyes[/eluser]
Hello,
Im starting to make a login module in codeigniter,
it gets authenticated,
but the thing is,
when I head back to the page where the login form is,
its still appears,
meaning the state of the user who logged in wasnt saved.

how do you do this in CodeIgniter? I did use the set_userdata() in the Session Class and I check it using the datas in there to check whether a user is log in or not but its does not works.


(( sorry for my bad english ))

here's my code :

Controller
Code:
<?php

class Site extends CI_Controller {

function index() {
  $data['is_logged_in'] = false;
  $this->load->view('includes\overall\header', $data);
  $this->load->view('includes\overall\footer');
}

function validate_credentials() {
  
  $this->load->model('members_model');
  $query = $this->members_model->validate();

  if($query)
  {
   $data = array(
    'username' => $this->input->post('username'),
    'is_logged_in' => true,
    'records' => $this->members_model->get_records()
    );

  
  
   $this->session->set_userdata($data);
   $this->load->view('includes\overall\header', $data);
   $this->load->view('includes\overall\footer');
  
  } else {
   $this->index();
  }

}

}

Model
Code:
<?php

class Members_model extends CI_Model{

function validate() {
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', md5($this->input->post('password')));
  $query = $this->db->get('membership');

  if($query->num_rows() == 1) {
   return true;
  }
}

function get_records()
{
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', md5($this->input->post('password')));
  $query = $this->db->get('membership');
  return $query->result();
}
}


this checks if the user is logged in or not,
if I enter the right username/password combination,
it will direct to the correct page that I am logged in.
But if I navigate back to the homepage,
the login form shows again. Sad

Code:
<aside>
&lt;?php ;
if(isset($is_logged_in)){
if ($is_logged_in == false){
  $this->load->view('includes\widgets\login');
} else
  $this->load->view('includes\widgets\logged_in');
}
?&gt;
</aside>
#2

[eluser]Sanjay Sarvaiya[/eluser]
you can get your session data in Codeigniter by using
Code:
$this->session->userdata('item');

farther more information please read this http://ellislab.com/codeigniter/user-gui...sions.html

Your view will be look like this.

Code:
if ($this->session->userdata('is_logged_in')){
  $this->load->view('includes\widgets\login');
} else {
  $this->load->view('includes\widgets\logged_in');
}
#3

[eluser]liesbetweenyoureyes[/eluser]
[quote author="Sanjay Sarvaiya" date="1338205512"]you can get your session data in Codeigniter by using
Code:
$this->session->userdata('item');

farther more information please read this http://ellislab.com/codeigniter/user-gui...sions.html

Your view will be look like this.

Code:
if ($this->session->userdata('is_logged_in')){
  $this->load->view('includes\widgets\login');
} else {
  $this->load->view('includes\widgets\logged_in');
}
[/quote]
thanks man, that worked, sorry If I didnt know that I just began CI a few days.
#4

[eluser]Sanjay Sarvaiya[/eluser]
As per proper CI Structure check session user data in your controller and conditionally load appropriate view from your controller.

In view files just display content.





Theme © iAndrew 2016 - Forum software by © MyBB