0

I am fairly new to CI and I am working my way through a project using it.

I am using CI 2.1.2 DataMapper 1.8.2.1.

I am trying to post a login form to http://www.example.com/userauthcontroller/login I am able to hit the function and it authenticates as it should. Mainly using the DataMapper examples and code.

Userauthcontroller.php:

public function login(){
    $params = $this->input->post();
    $url = $params['url'];
    $loginId = $params['loginid'];
    $password = $params['loginpass'];
    $u = new User();
    $u->username = $loginId;
    $u->pass_word =$password;
    if($u->login()){
        $confirmedUser = $u->get_where(array('id' => $u->id));
        $CI =& get_instance();
        $CI->session->set_userdata(array('currentuser' => $confirmedUser));
        //print_r($CI->session->userdata('currentuser'); // works
        //die();
        //tried this too
        //$this->session->set_userdata(array('currentuser' => $confirmedUser));
        redirect($url);
    }else{
        echo '<p>' . $u->error->login . '</p>';
    }
}

When I redirect to the welcome controller I can not find the currentuser in the session.

welcome.php:

function index(){
    $data['site_title'] = "My Site Title";
    $data['view_file'] = "layout_views/home_view";

    $CI =& get_instance();
    $user = $CI->session->userdata('currentuser');

    $data['currentuser'] = $user;
    print_r($user);
    print("<BR/>");
    print_r($data);
    die();
    //If I let this continue into my view $currentuser is not available.
    $this->load->view('index_view', $data);
}

home_view.php:

<p><? echo $currentuser ?></p> <!-- this does not print -->
<p><? echo $site_title ?></p>  <!-- this prints -->

application/config/autoload.php: (Relevant entries that I know of)

$autoload['libraries'] = array('database', 'datamapper', 'session');
$autoload['packages'] = array(APPPATH.'third_party/datamapper');

I have done a bit of searching on this ... and I even checked for spelling errors.

Any suggestions would be appreciated.

If more information is needed I will be checking back here frequently.

Thanks

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
John
  • 763
  • 1
  • 10
  • 21
  • 1
    Do `$CI->session->set_userdata('id', $u->id)` and `$CI->session->userdata('id')` work? – Yan Berk Jul 20 '12 at 06:26
  • It appears that I can pass the id in the session. Is there something preventing me from passing the model object? – John Jul 20 '12 at 12:24

1 Answers1

0

Thanks to Yan, it got me headed in the right direction. I don't know if this the best way to do it, but it is working.

userauthcontroller.php:

public function login(){
    $params = $this->input->post();
    $url = $params['url'];
    $loginId = $params['loginid'];
    $password = $params['loginpass'];
    $u = new User();
    $u->username = $loginId;
    $u->pass_word =$password;
    if($u->login()){
        $confirmedUser = new User(array('id' => $u->id));
        $CI =& get_instance();
        $CI->session->set_userdata(array('currentuser_id' => $u->id));
        $CI->session->set_userdata(array('currentuser' => $confirmedUser));
        redirect($url);
    }else{
        echo '<p>' . $u->error->login . '</p>';
    }
}

welcome.php:

    function index(){
    $data['site_title'] = "PrimitiveSurvival.com";
    $data['view_file'] = "layout_views/home_view";
    $CI =& get_instance();
    $u = new User($CI->session->userdata('currentuser_id'));
    if($u->id != null){
        $u->group->get_iterated();
        $data['currentuser'] = $u;
    }else{
        $data['currentuser'] = "";
    }
    $this->load->view('index_view', $data);
}

home_view.php:

 <?php
  if($currentuser != ""){
     print($currentuser->username);
  }else{
     print("User not logged in.");
  }

If there is a better way or something I am missing, please by all means improve this answer. ?>

John
  • 763
  • 1
  • 10
  • 21
  • Your way seems fine. Why do you set `currentuser` in the session if you do't use it? Did you try to use `$this` instead of CI? In the controllers\models there is no need to get the instance first. It is loaded automatically if you extend CI_Controller and CI_Model respectivly. – Yan Berk Jul 20 '12 at 18:40
  • I am fairly new to CI ... These bits of code were just to make sure I could get the information I need. The project I am working on is going to get the users permissions and groups from the currentuser object throughout the application. – John Jul 22 '12 at 23:55