duanjurong1347 2013-08-27 06:59
浏览 25

Codeigniter中的会话变量不起作用

Hi i have created a session in Codeigniter i have set the session variable in my model. But after setting the session variable value when i call session variable in my view through Controller the session variable value becomes null. Any help???

Update

This is my Model where i set my session variable

function login ($username, $password)

    {
        $this->db->where('username' , $username);
        $this->db->where('password', $password);

        $query = $this->db->get('users');

        if ($query->num_rows()>0)

        {
            foreach ($query->result() as $rows)
            {
                $data = array(

                    'user_name' => $rows->username,
                    'logged_in' => TRUE,
                    'validated' => true
                );
            }

                $this->session->set_userdata($data);
                //$user = $rows->username;
                //$this->session->set_userdata('user_name', $user);

                return true;
        }

        else

        {
            return false;
        }

    }

Here is my controller from where i redirect to the view

public function verification()

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


        $result = $this->site_model->login($username, $password);


        //$result = $this->session->set_userdata('validated');

        if ($result)
        {
            //$this->admin();
            //$this->session->set_userdata('login_state', TRUE);

            redirect ('site/index');

        }
        else
        {

            redirect ('site/login');
            //$this->load->view('login');
        }
    }

i have called the session_start(); in controller under construct();

and this is how i access the session variable in my view

<?php if ($this->session->userdata('user_name') != "") { ?>

.....
  • 写回答

10条回答 默认 最新

  • dongzz4545 2013-08-27 07:41
    关注

    use autoload applications/config/autoload.php, to add session liblary and remove session_start();

    $autoload['libraries'] = array('session');
    

    Model pass post data vithout varibles, you can do like that:

    controller:

    public function verification()
    {
         $result = $this->site_model->login();
    
          if ($result){
              redirect ('site/index');
          } else {
              redirect ('site/login');
          }
    }
    

    Model:

    public function login()
    {
        $this->db->where('username' , $this->input->post('username'));
        $this->db->where('password', $this->input->post('password'));
    
        $query = $this->db->get('users');
    
        if ($query->num_rows()>0)
    
        {
            $res = $query->row_array();
    
            $data = array(
                'user_name' => $res['username'],
                'logged_in' => TRUE,
                'validated' => true
            );
    
    
            $this->session->set_userdata($data);
            return true;
        } else {
            return false;
        }
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?