session userdata不显示管理员值

时间:2017-04-15 18:17:35

标签: codeigniter

在数据库中,我有用户名,密码,ID,管理员等,但我的管理员值不显示。为什么? http://prntscr.com/ewuiz9 http://prntscr.com/ewuj4o

       // Get username
        $username = $this->input->post('username');
        // Get and encrypt the password
        $password = md5($this->input->post('password'));

        // Login user
        $user_id = $this->user_model->login($username, $password);

        if($user_id){
            // Create session


            $user_data = array(
                'admin' => $admin,
                'user_id' => $user_id,
                'username' => $username,
                'logged_in' => true
            );

            $this->session->set_userdata('logged_in', $user_data);

            // Set message
            $this->session->set_flashdata('user_loggedin', 'You are now logged in');

            redirect('posts');
        } else {
            // Set message
            $this->session->set_flashdata('login_failed', 'Login is invalid');

            redirect('users/login');
        }
    }
}

2 个答案:

答案 0 :(得分:0)

  

因为您已将其设置为

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <mvc:annotation-driven />
    <context:component-scan base-package="com.controllers" />


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    </bean>


</beans>
  

您可以像在其他控制器上一样访问它。

 $user_data = array(
    'admin' => $admin,
     'user_id' => $user_id,
    'username' => $username,
    'logged_in' => true
 );

 $this->session->set_userdata('logged_in', $user_data);
  

如果您只是想要访问

$userdata = $this->session->userdata('logged_in'); echo $userdata['username'];

然后你只需将它设置为

echo $this->session->userdata('username');

确保您拥有自动加载会话,以便可以通过网站访问

配置/ autoload.php

 $user_data = array(
    'admin' => $admin,
     'user_id' => $user_id,
    'username' => $username,
    'logged_in' => true
 );

 $this->session->set_userdata($user_data);

在config.php上,确保已设置会话路径和会话文件夹权限0700

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

答案 1 :(得分:0)

谢谢。 在模型中我发现:

    // Log user in
    public function login($username, $password){
        // Validate
        $this->db->where('username', $username);
        $this->db->where('password', $password);

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

        if($result->num_rows() == 1){
            return $result->row(0)->id;
        } else {
            return false;
        }
    }

我改为:

    // Log user in
    function login($username, $password){
        $this->db->select('id,admin,username,password,email');
        $this->db->from('users');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get();
        if($query->num_rows()==1){
            return $query->result();
        } else{
            return false;
        }
    }