douying4909 2013-09-11 10:23
浏览 51
已采纳

如何在代码点火器中使用会话创建权限

I have a problem to create permission menu,

Oke I have a table user..

username, password, permission.

I have a function in model like this..

function valid_login($username, $password)
        {
        $query = $this
            ->db
            ->where('username', $username) 
            ->where('password', md5($password)) 
            ->limit(1) 
            ->get('user'); /

        if ($query->num_rows() == 1) {
                return $query->row_array(); 
        }
        else
                {
                        return FALSE; 
                }
        }

The value post from controller, The Script like this..

public function index()
        {
              if ( isset($_SESSION['permission']) ) { 
                 redirect('home_controller/home'); 
              }

              $this->load->library('form_validation'); 
              $this->form_validation->set_rules('username', 'Username', 'required'); 
              $this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
              if ( $this->form_validation->run() == TRUE ) { 
                 $this->load->model('m_user'); 
                 $result = $this->m_user->valid_login(
                             $this->input->post('username'),  
                             $this->input->post('password')
                          );

                        if ($result == TRUE) { 
                        $_SESSION['permission'] = ????;
                            redirect('home_controller/home'); 
                        }
              } 

                $this->load->view('login_form'); 
        }

I dont understand, how to get field permission value from model. I need that value to create $_SESSION..

And myView like this..

<?php
if($_SESSION['permission']=="Administrator"){
    $this->load->view('administrator_menu');;
}else{
    $this->load->view('user_menu');;
}
?>

Thanks for the attention.. Anyone can help me..

  • 写回答

1条回答 默认 最新

  • douqi1931 2013-09-11 10:25
    关注

    Like this to set session in CI:

    if ($result == TRUE) { 
        print_r( $result );die;
        //$this->sesson->set_userdata('logged_user', $result); #this to set the session with the returned result
        $_SESSION['permission'] = $result['permission'];  #set permission from the result in the session
        redirect('home_controller/home'); 
    }
    

    Now to read the session if exists or not do like this:

    if( $this->session->userdata('logged_user') ){
        echo "The user id stored in session is :".$id = $this->session->userdata['logged_user']['id'];
    }else{
        redirect('someplace', 'refresh');
    }
    

    EDIT :
    To start the session first autoload it in application/autoload.php:

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

    Secondly set a random encryption key in application/config.php like this:

    $config['encryption_key'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么