Codeigniter按权限显示额外数据

时间:2015-02-20 11:06:17

标签: php mysql codeigniter privileges privilege

好的,我使用过CodeIgniters注册/登录表单。但我有一个项目菜单,其中一些选项只有在您的权限== 1时才会显示。我使用现有数据库并添加了权限列,当您为新用户创建此值为0时,如果您的管理员将更改到1。

我的问题:

我有这个菜单

<div class="menu_rechts">
<ul>
<?php 
error_reporting(-1);
ini_set('display_errors',1);
  $this->load->model('user_model');
  if($this->session->userdata('privilege')== "1") {
echo "test";
} else {
    echo "test2";?>
<a href="<?php echo base_url('index.php/user/index'); ?>"><li>Login/Register</li></a>
</ul>
</div>

它包含在标题

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
 public function __construct()
 {
  parent::__construct();
  $this->load->model('user_model');
 }
 public function index()
 {
  if($this->session->userdata('logged_in')=== TRUE)
  {
   $this->welcome();
  }
  else{
   $data['title']= 'Home';
   $this->load->view('header',$data);
   $this->load->view("registration.php", $data);
   $this->load->view('footer',$data);
  }
 }
 public function welcome()
 {
  $data['title']= 'Welcome';
  $this->load->view('header',$data);
  $this->load->view('welcome.php', $data);
  $this->load->view('footer',$data);
 }
 public function login()
 {
  $email=$this->input->post('email');
  $password=md5($this->input->post('pass'));

  $result=$this->user_model->login($email,$password);
  if($result) $this->welcome();
  else        $this->index();
 }
 public function thank()
 {
  $data['title']= 'Thank';
  $this->load->view('header',$data);
  $this->load->view('thank.php', $data);
  $this->load->view('footer',$data);
 }
 public function registration()
 {
  $this->load->library('form_validation');
  // field name, error message, validation rules
  $this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
  $this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
  $this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');

  if($this->form_validation->run() == FALSE)
  {
   $this->index();
  }
  else
  {
   $this->user_model->add_user();
   $this->thank();
  }
 }
 public function logout()
 {
  $newdata = array(
  'user_id'   =>'',
  'user_name'  =>'',
  'user_email'     => '',
  'privilege'     => '',
  'logged_in' => FALSE,
  );
  $this->session->unset_userdata($newdata );
  $this->session->sess_destroy();
  $this->index();
 }    
}
?>

最后是模型

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class User_model extends CI_Model {
     public function __construct()
     {
      parent::__construct();
     }
     function login($email,$password)
     {
      $this->db->where("email",$email);
      $this->db->where("password",$password);

      $query=$this->db->get("user");
      if($query->num_rows()>0)
      {
       foreach($query->result() as $rows)
       {
        //add all data to session
        $newdata = array(
  'user_id'  => $rows->id,
  'user_name'  => $rows->username,
  'user_email'    => $rows->email,
  'privilege'    => $rows->privilege,
  'logged_in'  => TRUE,
);
       }
       $this->session->set_userdata($newdata);
       return true;
      }
     return false;
     }
     public function add_user()
     {
      $data=array(
        'username'=>$this->input->post('user_name'),
        'email'=>$this->input->post('email_address'),
        'password'=>md5($this->input->post('password'))
      );
      $this->db->insert('user',$data);
     }
    }
    ?>

我想要回显测试,只有登录用户的权限== 1

0 个答案:

没有答案