0

我对 CI 相当陌生,我正在使用它完成一个项目。

我正在使用 CI 2.1.2 DataMapper 1.8.2.1。

我正在尝试将登录表单发布到http://www.example.com/userauthcontroller/login 我能够点击该功能并按应有的方式进行身份验证。主要是使用DataMapper的例子和代码。

用户验证控制器.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>';
    }
}

当我重定向到欢迎控制器时,我在会话中找不到当前用户。

欢迎.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:(我知道的相关条目)

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

我对此进行了一些搜索……我什至检查了拼写错误。

任何建议,将不胜感激。

如果需要更多信息,我会经常回来查看。

谢谢

4

1 回答 1

0

感谢Yan,它让我朝着正确的方向前进。我不知道这是否是最好的方法,但它正在工作。

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>';
    }
}

欢迎.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.");
  }

如果有更好的方法或我遗漏的东西,请务必改进此答案。?>

于 2012-07-20T16:17:15.193 回答