Codeigniter会话问题破坏/创建

时间:2014-09-16 16:40:38

标签: php codeigniter session codeigniter-2

我正在从Codeigniter的1.7.3升级到2.2.0。我的旧登录脚本有效,但现在不行了。为什么sess_destroy导致了这个?

这项工作在新版本的codeigniter中:

        $this->session->sess_destroy();
        $this->session->unset_userdata( array( 'logged' => false, 'users_id' => false, 'teams_id' => false, 'leader_teams_id' => false ) );
        $this->session->sess_create();              
        $this->session->set_userdata( $data );

这在新版本的codeigniter中不起作用:

        $this->session->sess_destroy();
        $this->session->unset_userdata( array( 'logged' => false, 'users_id' => false, 'teams_id' => false, 'leader_teams_id' => false ) );         
        $this->session->set_userdata( $data );

所以基本上$this->session->sess_create();让它起作用,但为什么会这样,并且这样使用它也是正确的吗?

这是完整的登录功能,因为它在旧版本的codeigniter中运行:

function login_do()
{
    if ( $_POST['username'] && $_POST['password'] )
    {
        $user = $this->db->where( array( 'users_name' => trim( $_POST['username'] ), 'users_password' => sha1( md5( $_POST['password'] ) ) ) )->get( 'users' );
        if ( $user->num_rows == '1' )
        {
            $user = $user->row();
            $data = array( 'logged' => true, 'users_id' => $user->users_id );
            if ( $user->users_teams_id > '0' )
            {
                $leader = $this->db->where( array( 'team_leader_id' => $user->users_id, 'teams_id' => $user->users_teams_id ) )->get( 'teams' )->row();
                $data['teams_id'] = $user->users_teams_id;
                if ( $leader )
                    $data['leader_teams_id'] = $user->users_teams_id;
            }

            $this->session->sess_destroy();
            $this->session->unset_userdata( array( 'logged' => false, 'users_id' => false, 'teams_id' => false, 'leader_teams_id' => false ) );
            $this->session->set_userdata( $data );
        }
        else
            $this->session->set_flashdata( 'error', 'user_pass' );
    }
    else
        $this->session->set_flashdata( 'error', 'data' );

    redirect( 'home' );
}

1 个答案:

答案 0 :(得分:0)

Codeigniter会话创建:

$data = array(
       'logged'   => $logged,
       'users_id' => $users_id,
       'teams_id'  => $teams_id,
       'leader_teams_id'=> $leader_teams_id
    );
 $this->session->set_userdata($data);

Codeigniter会话破坏:

$array_items = array('logged' =>'', 'users_id'=>'', 'teams_id'=>'', 'leader_teams_id'=>'');    
    $this->session->unset_userdata($array_items);