通过代码点火器创建会话并使用唯一id登录用户的最佳方式


Optimal way to create a session and login a user with unique id via codeigniter

我一直在代码点火器中重建我的社交网络,并正在寻找一种在用户登录时以登录状态启动会话的最佳方式(可能与使用代码点火器会话类相反?因为我想使用他们创建帐户时创建的id)

我已经能够创建一个用户,对他们的密码进行散列和加盐处理,并使用散列后的密码将他们登录。

我的网站将有很多"登录"answers"注销"视图,这些视图因用户是否登录而有所不同。

也就是说,在登录时,我想用注册时创建的id启动一个登录会话。

这是我的控制器,它记录用户减去我试图做的事情

function validate_credentials()
{
    $this->load->model('user_model', 'um');
    $login = $this->input->post('submit');
        $this->load->library('encrypt');
    if($login) {
        $user = $this->um->
                    validate(array('email' => $this->input->post('email')));
        if( $user ) {
            if($user->password == $this->encrypt->sha1( $user->salt .
                         $this->encrypt->sha1($this->input->post('password')))) {
                  $this->session->set_userdata( array('email' => 
                                   $this->input->post('email')
                  ));
                  redirect('account/dashboard');
                  exit;
            }
        }
    }
    $this->index();
}
/*---------- EDIT ----------*/
Getting these errors:

  Message: Cannot modify header information - headers already sent by (output started at /Users/mycomputer/Sites/cl_ci_new/application/controllers/auth.php:44)
    Filename: libraries/Session.php
    Line Number: 672
    Message: Cannot modify header information - headers already sent by (output started at /Users/mycomputer/Sites/cl_ci_new/application/controllers/auth.php:44)
    Filename: helpers/url_helper.php
    Line Number: 542
and here are those lines in my code or the Session.php file:
    // Set the cookie
        setcookie(
            this->sess_cookie_name,
            $cookie_data,
            $expire,
            $this->cookie_path,
            $this->cookie_domain,
            $this->cookie_secure
                );

和url_helpers.php:

switch($method)
        {
            case 'refresh'  : header("Refresh:0;url=".$uri);
                break;
            default         : header("Location: ".$uri, TRUE, $http_response_code);
                break;
        }

我不明白问题出在哪里…

验证成功后,在会话中保存变量:

if ($user->password == ...) {
     $this->session->set_userdata('logged_in', TRUE);
}
else {
     $this->session->set_userdata('logged_in', FALSE);
}

稍后,需要时,检查:

if ($this->session->userdata('logged_in')) {
}