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;
        }
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题