doushua7737 2018-10-18 23:35
浏览 105
已采纳

Codeigniter - 使用Userdata进行数据库查询

I want to use Session data as a condition for a query in database, but it only returns NULL. I have tried $this->session->userdata('account'), but it still won't work.

Function - Login/Set Userdata :

    public function login_auth()
  {
        $this->load->helper('security');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        if ($this->form_validation->run() == FALSE) {
            if(isset($this->session->userdata['logged_in'])){
                $this->dashboard();
            }else{
                $this->index();
            }
        } else {
            $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')
            );
            $result = $this->agent_model->login($data);
            if ($result == TRUE) {
                $username = $this->input->post('username');
                $result = $this->agent_model->read_user_information($username);
                if ($result != false) {
                    $session_data = array(
                    'username' => $result[0]->username,
                    'owner' => $result[0]->owner,
                    'account' => $result[0]->account,
                    'id' => $result[0]->id
                    );
                    $this->session->set_userdata('logged_in', $session_data);
                    $this->dashboard();
                }
            } else {
                $data = array(
                'error_message' => 'Invalid Username or Password'
                );
                $this->load->view('header');
                $this->load->view('pages/login', $data);
                $this->load->view('footer');
            }
        }
  }

Function - Using Userdata->Account as a condition(From Another Function)

$sess_account = $this->session->userdata('account');
    var_dump($this->session->userdata('account'));
    $coords = $this->map_model->get_coordinates($sess_account);

Am i missing something here? Any help is truly appreciated. Thank you!

  • 写回答

1条回答 默认 最新

  • dt102282 2018-10-19 01:06
    关注

    If you are certain that the session item 'logged_in' should have been set and $this->session->userdata['logged_in'] is returning null it is likely you do not have session configured correctly.

    It's almost always improper $config value for session and or cookie items.

    Here is a git repo that should help you test your setup.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!