0

我是 codeigniter 和 php 的新手,只有几天,所以我需要一点帮助。我正在尝试将一些数据从表中放入我的 cookie 中,以便我可以检查登录后将用户重定向到哪里。在表users中有两列命名AdminCompany如果用户是或不是,则为一或零,然后我希望将该信息插入 cookie。

user_controler 中的函数构造为:

function conformation(){
    $this->load->model('user');
    $q = $this->user->confr();
    if($q){
        $data = array(
            'username' => $this->input->post('username'),
            'Admin' => $this->input->post($a = $this->user->getAdmin), // get 1/0 from users column Admin 
            'Company' => $this->input->post($c = $this->user->getComp),            
            'login' => true
        );
        if( $a == 1 ){ //is admin redirect to admin view 
           $this->session->set_userdata($data);
        redirect('user_controler/useradm');
        }
        if($c == 1){ //if company redirect to company view
          $this->session->set_userdata($data);
        redirect('user_controler/usercomp');
        }
        $this->session->set_userdata($data);// if common user redirect to user view
        redirect('user_controler/userpro');
    }
    else{ // if nothing above redirect to login page
        redirect('user_controler/log');
    }
}

在用户模型中:

function getAdmin{
   $this->db->where('Admin', 1);
   $a = $this->db->get('users');
}
function getComp{
   $this->db->where('Company', 1);
   $a = $this->db->get('users');
}
function conf(){
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', $this->input->post('password'));
        $q = $this->db->get('users');

        if($q->num_rows == 1 ){
            return TRUE;                
        }
    }

还有用于检查登录的站点控制器

class Site extends CI_Controller{

    function __construct() {
        parent::__construct();
        $this->login();
    }

    function login(){
        $login = $this->session->userdata('login');

        if(!isset($login) || login != TRUE){
            $this->log;
            die();
        }
    }

}

当然它不起作用,因为我可能应该以其他方式检查这些列,但我不知道如何。我还启用了 table ,它在没有andci_session的情况下可以完美工作。AdminCompany

4

1 回答 1

0

您好,欢迎来到 Stackoverflow。

这是我对代码的更新(我已经注释了我的更改):

function conformation(){
    $this->load->model('user');

    if($this->user->confr()){  //$q wasn't needed, as you are only using this twice
        $user = $this->input->post('username');  //I have added this as I will be referring to it a couple of times.
        $data = array(
            'username' => $user,
            'Admin' => $this->user->getAdmin($user), // Your method was questioning the original form looking for data that it would never find - This will question your model.
            'Company' => $this->user->getComp($user), //Same as above            
            'login' => true
        );
        $this->session->set_userdata($data);  //It doesn't matter who the user is, we shall set the data to start with.
        if($this->user->getAdmin($user)){ //is admin redirect to admin view 
           redirect('user_controler/useradm');
        }
        elseif($this->user->getComp($user)){ //if company redirect to company view
            redirect('user_controler/usercomp');
        }
        else { //Redirect non-privileged users.
            redirect('user_controler/userpro');
        }
    }
    else{ // if nothing above redirect to login page
        redirect('user_controler/log');
    }
}

用户型号:

function getAdmin($user){
   $this->db->where('username', $user); //Before you was just returning everyone who is an admin   This instead finds the user
   $a = $this->db->get('users');

   foreach($a as $u) {
       if($u["Admin"]==1) { return true; }  //This finds if the user is a admin or not, and the function will now return a value (true)
   }
}
function getComp($user) {
   $this->db->where('username', $user); 
   $a = $this->db->get('users');

   foreach($a as $u) {
       if($u["Company"]==1) { return true; }
   }
}  //Edited similar to the function above
function conf(){
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $q = $this->db->get('users');

    if($q->num_rows == 1 ){
        return TRUE;                
    }
}

最后是您的登录功能:

function login(){
    $login = $this->session->userdata('login');

    if(!isset($login) || $login != TRUE){  //You weren't referring to your $login variable
        $this->log;
        die();
    }
}

希望这对您的问题有所帮助,如果您需要任何修改,请告诉我。

于 2013-05-18T15:12:12.537 回答