与多维数组

时间:2018-05-13 17:19:01

标签: php codeigniter

检查模型中的用户凭据并在那里设置userdata并且用户已成功登录但在视图中我想显示登录的用户名。但我无法在视图中传递登录的值。始终在视图文件中返回计数值0。但是我不知道为什么我在调用会话数据时无法获得价值。请提前感谢您的帮助。 我的型号代码:

function auth($email, $password, $remember=false)
{
    // make sure the username doesn't go into the query as false or 0
    if(!$email)
    {
        return false;
    }

    $this->db->select('*');
   // $this->db->where('active', 1);
    $this->db->where('email', $email);
    $this->db->where('password',  sha1($password));
    $this->db->limit(1);
    $result = $this->db->get('guests');
    $result = $result->row_array();

    if (sizeof($result) > 0)
    {
        $admin = array();
        $admin['front_user'] = array();
        $admin['front_user']['id'] = $result['id'];
        $admin['front_user']['firstname'] = $result['firstname'];
        $admin['front_user']['lastname'] = $result['lastname'];
        $admin['front_user']['email'] = $result['email'];
        $admin['front_user']['mobile'] = $result['mobile'];

        if($remember)
        {
            $loginCred = 
            json_encode(
            array('username'=>$username, 'password'=>$password));
            $loginCred = base64_encode($this->aes256Encrypt($loginCred));
            //remember the user for 6 months
            $this->generateCookie($loginCred, strtotime('+6 months'));
        }

        $this->session->set_userdata($admin);
        return $admin['front_user'];
        return true;
    }
    else
    {
        return false;
    }
}

我的控制器代码:

function login(){
//echo '<pre>'; print_r($_POST);die;
$this->load->library('form_validation');
$this->form_validation->set_rules
('email', 'Email',    'trim|required|max_length[32]');
$this->form_validation->set_rules
('password', 'Password', 'required|min_length[4]');

if ($this->form_validation->run() == TRUE)
{

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

return  $return     =     $this->login_model->auth
($email,$password,'','');
    if($return){
        echo 1;die;
    }else{
        echo 'Email or Password invalid';
    }

   }
 else{
    echo validation_errors();
 }
}

我的观点文件:

    <?php
    if(count($this->front_user)>0):?>
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" >
    <?php echo $this->front_user['firstname']?> 
    </a>
    <a href="<?php echo site_url('front/account/logout')?>" >
    <i class="fa fa-sign-out pull-left"></i> 
    <?php echo lang('logout')?> 
    </a>
    <?php endif; ?>
    <?php if(count($this->front_user)<1):?>
    <a href="#" >
    <?php echo lang('login')?>
    </a>
    <a 
    <?php echo lang('signup')?></a></li>
    <?php endif;
    ?> 
This is my primary view loading method in controller
    function index()
    {
        $data['meta_description']   =   $this->setting->meta_description;
        $data['meta_keywords']      =   $this->setting->meta_keywords;
        $data['page_title']     = lang('home');
        $data['banners']        = $this->homepage_model->get_banners();
        $data['testimonials']   = $this->homepage_model->get_testimonials();    
        $data['room_types']     = $this->homepage_model->get_room_types();
        $data['coupons']        = $this->homepage_model->get_coupons();     
        //$data['testimonials'] = $this->testimonial_model->get_all();
            //echo '<pre>'; print_r($data['coupons']);die;
        $this->render('homepage/homepage', $data);      
    }
   And I send login credentials via ajax request and get value into that
   view via ajax
     $( "#signinForm" ).submit(function( event ) {
    event.preventDefault();
    var form = $(form).closest('form');
    call_loader();
    $.ajax({
        url: SITE_URL+'/front/homepage/login',
        type:'POST',
        data:$("#signinForm").serialize(),
        success:function(result){
        //alert(result);return false;
        console.log(result);
              if(result==1)
                {
                    toastr.success('You Logged In Successfully');
                    //location.reload(); 
                    window.location.reload()
               }
                else
                {
                    remove_loader();
                    toastr.error(result);
                    //$('#err').html(result);
                }

         }
      });
});
this is java script code.

1 个答案:

答案 0 :(得分:-1)

If($this->session->has_userdata($array['front_user']){
     echo $this->session->userdata($array['front_user']['first_name']);
}