Codeigniter会话无法使用我的代码

时间:2016-12-16 06:02:13

标签: php ajax codeigniter session

我在codeigniter中遇到此问题,但没有显示任何内容。我用ajax调用结果。如果我将它指向echo,则只显示正确的用户ID。我不知道我错了吗?我希望有人会纠正我。谢谢

这是我的用户登录功能。这用于为当前用户登录设置会话

{
  "responseData": {
    "status": 1,
    "message": "",
    "result": [
      {
        "Order": {
          "id": "677",
          "detail_location_instructions": "Near Inox"        
        },
        "OrderDetail": [
          {
            "order_id": "677",
            "delivery_address": "Smart Club Gimnasio - Avenida Álvarez Thomas, Buenos Aires, Autonomous City of Buenos Aires, Argentina"           
          },
          {           
            "order_id": "677", 
            "delivery_address": "Lower Fort Street, Dawes Point, New South Wales, Australia"          
          }
        ]
      },
      {
        "Order": {
          "id": "680"
        },
        "OrderDetail": []
      },
      {
        "Order": {
          "id": "684"         
        },
        "OrderDetail": [
          {           
            "order_id": "684",          
            "delivery_address": "Four Seasons - Posadas"           
          }
        ]
      }
    ]
  }
}

这是会话不起作用的功能。调用当前用户登录ID

 public function getOrderlist(){           
            if($this->processRequest){
                $err = false; 
                if(empty($this->requestData['id'])){
                    $this->responceData['message'] = "Please provide User ID";
                    $err = true;
                } 
                if(!$err){ 
                    $id = trim($this->requestData['id']);
                    $conditions = array('Order.user_id'=>$id); 
                    $data = $this->Order->find('all',array('conditions'=>$conditions)); 
                    if(!empty($data)){   
                        $c = array();
                        foreach ($data as $key => $value) { 
                            $c[] = array(
                                'Id' => $value['Order']['id'],
                                'deliveryAddress' => $value['OrderDetail']['delivery_address']                                                  
                            );                                
                        } 
                    }
                    $this->responceData['result'] = $c;
                    $this->responceData['status'] = 1;
                }
            }
        }

}

这是在View中显示的Ajax函数。在这个例子中,我将用户个人资料图片调用显示在视图中。

public function login(){

    $username = $this->input->post('username');
    $password = $this->input->post('password');

        $sql= $this->crud->getData("
                tbl_user","
                user_username='$username' AND 
                user_password=md5('$password')
                LIMIT 1");

     if($sql['total']==1) { 

            $user_sess = array();

                 foreach($sql['rows'] as $row){
                     $user_sess = array('user_id'=>$row->user_id,
                                       'user_name'=>$row->user_username,
                                       'user_role'=>$row->user_role,
                                       'profile_id'=>$row->profile_id,
                                       'user_type'=>$row->user_type,
                                       'user_status'=>$row->user_status,
                                       'user_group_id'=>$row->user_group_id,
                                       'user_sub_group_id'=>$row->user_sub_group_id);
                 }            

            $this->session->set_userdata('user_sess', $user_sess); 

            $result['login']='valid';
       }else{

            $result['login']='invalid';

    }//end if else

    echo json_encode($result);

}/*END LOGIN FUNCTION*/ 

谢谢。抱歉我的英文不好

1 个答案:

答案 0 :(得分:0)

您要定义会话数据的数组,用户ID设置为user_id

因此,在user_current_details()函数中,您将获得如下的用户ID:

$user_id=$this->session->userdata('user_id');

而不是

$user_id=$this->session->userdata('user_sess');

阅读文档以获取更多帮助:https://www.codeigniter.com/user_guide/libraries/sessions.html#retrieving-session-data