douan4106 2017-03-18 21:28
浏览 39

codeigniter会话用户数据设置

i want to set user data in session. i am able to set email in session data as i am getting it from login form. But i dont know how to get all details from database and set to session. here is my code

Controller:

public function login_validation() {

    // get form input
    $email = $this->input->post("email");
    $password = $this->input->post("password");

    // form validation

    $this->load->library('form_validation');
    $this->form_validation->set_rules("email", "Email-ID", "trim|required");
    $this->form_validation->set_rules("password", "Password", "trim|required");

    if ($this->form_validation->run() == FALSE)
    {
        // validation fail
        $this->load->view('admin/login');
    }
    else
    {
        // check for user credentials
        $this->load->model('site_model');
        $uresult = $this->site_model->get_user($email, $password);
        $data["results"]=$this->site_model->getdata_user($email);
        foreach ($data as $row) {
            $fname = $row['fname'];

        }

        if (count($uresult) > 0)
        {

            $data = array(                      
                'email' => $email,
                'fname' => $fname,
                'lname' => $lname,  
                'pre'=>$pre,
                'is_logged_in' => 1                         
            );

            // set session
            $this->session->sess_expiration = '0';// will not expire
            $this->session->set_userdata($data);

            redirect("admin/index");
        }
        else
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Wrong Email-ID or Password!</div>');
            $this->load->view('admin/login');
        }
    }

}

The model:

function getdata_user($email)
{
    $query= $this->db->get_where("users", array("email"=>$email));

    return $query->result();

}

there are some fileds in users table: fname,lname that i want to set to session.

thank you in advance.

  • 写回答

3条回答 默认 最新

  • douyou1857 2017-03-18 21:33
    关注

    Take a look at the query builder class. https://www.codeigniter.com/userguide3/database/query_builder.html

    Using this resource you should be able to construct some code that reads the columns in the database.

    Example on how to select columns with a where clause.

    $this->db->select('fname, lname'); 
    $this->db->from('users');
    $this->db->where('email', $email);
    $query = $this->db->get();
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度