首页 > 解决方案 > user_session 不工作(Codeigniter,Ajax)

问题描述

新手在这里。我有一个ajax登录功能。登录没有问题,但会话存在。我遇到的问题是每当我调用会话时,它都不起作用。我用 print_r($this->session->all_userdata()); 查看 user_session 数据,但我没有得到任何信息。有什么我错过的吗?任何帮助将不胜感激。太感谢了。

在此处输入图像描述

意见:

阿贾克斯:

<script>
    $(function(){
        
        $("#doLogin").submit(function(e){
            e.preventDefault();


                
            $("#btnSubmit").css('display', 'none');
            $("#submit_preloader").css('display','block');
            $("#errorMessage").html('');

            $.ajax({
                url: "<?=site_url('account/dologin')?>",
                data: $(this).serialize(),
                dataType: "json",
                type: "post",
                async: false,
                success: function(data){

                    if(data.response == "true") {

                        $("#doLogin")[0].reset();
                        
                        $("#submit_preloader").css('display','none');

                        $("#errorMessage").css('display', 'block');
                        $("#errorMessage").html(data.errors);
                        
                            setTimeout("window.location.href='<?=site_url('profile/prof')?>'" ,300);
                        } else {

                            
                            $("#errorMessage").css('display', 'block');
                            $("#errorMessage").html(data.errors);

                            //alert("SORRY. " + data.errors);

                            $("#btnSubmit").css('display','block');
                            $("#submit_preloader").css('display','none');

                            
                        }
                    
                    },
                        
                });
                        
        });
        
    }); 

</script>

控制器:

public function dologin()
    {
        
        //die('if tinawag to');
        $this->output->enable_profiler(false);
        $this->form_validation->set_rules('formUsername','Username','required|min_length[8]|max_length[35]');
        $this->form_validation->set_rules('formPassword','Password','required|min_length[8]|max_length[35]');
        
        $success = $this->form_validation->run($this);
        
        
        if($success)
        {
            $pdata = array(
                    'username' => trim($this->input->post('formUsername')),
                    'password' => MD5(trim($this->input->post('formPassword'))),
                    
            );
            
            
            //          $data['response'] = "false";
            //          $data['errors'] = 'Processing';
            
            if($this->accounts->checkUsername(trim($this->input->post('formUsername')))) {
                
                $user = $this->accounts->checkUserData($pdata);
                
                if($user){
                    
                    $this->accounts->log_session();
                    
                    //log user
                    
                    $data['response'] = "true";
                    $data['errors'] = 'login successful';
                    
                    
                } else {
                    $data['response'] = "false";
                    $data['errors'] = "Invalid User Password";
                }
                
            } else {
                $data['response'] = "false";
                $data['errors'] = "Username does not exist.";
            }
        
        
    } else {
        $data['response'] = "false";
        $data['errors'] = validation_errors();
        
        
    }
    
    echo json_encode($data);
}

Model: // User_session 数据

   function checkUserData($data)
    {
        $sql = "select * from users where username like ? and password like ?";
        $Q = $this->db->query($sql, array($data['username'], $data['password']));
        
        if($Q->num_rows() > 0) {
            
        $R = $Q->row_array();
                
        $arrSession = array(
                'uid' => $R["userID"],
                'username' => $R["username"],
                'mobile' => $R["mobile"],
                'password'=> $R["password"],
                'currentPoints' => $R['currentPoints'],
                'mobile' => $R['mobile'],
                'firstname' => $R['firstname'],
                'lastname' => $R["lastname"],
                'email' => $R["email"],
                'account_type' => $R["account_type"],
              
                'commCurrentBalance' => ["commCurrentBalance"]
        );
        
        $this->session->set_userdata($arrSession);
        
        
        //print_r( $this->session->all_userdata()); 
        
            return $R;
        } else {
            return false;
        }
        
    }

标签: ajaxcodeigniter

解决方案


您需要在控制器的 __construct 函数中包含会话库。


推荐阅读