dtdt0454 2017-08-14 09:41
浏览 62
已采纳

在codeigniter中授权

I'm working with codeigniter, I'm doing an auth system - user can log in, he can see all pages, after he can log out. The problem is next: why logged out user can see all pages without to be logged in. how can I correct it? This is my controller:

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->helper(array('url', 'form'));
    $this->load->model("usermodel");
    $this->load->library('session');
}

private function view($page, $data=false) {
  if($page == "auth/login" ){
        $this->load->view("auth/header_auth.php");
    }else{
        $this->load->view("header.php");
  }

  $this->load->view($page, $data);
  $this->load->view("footer.php");
}

public function index() {
  if ($this->session->userdata("user")) {
      redirect("dashboard", "refresh");
      return;
  }
  $this->view("auth/login");
}

public function fail() {
  $this->view("auth/login");

}

public function dashboard() {
  $this->view("auth/dashboard");
}

public function login() {
  $login = $this->input->post("login");
  $password = $this->input->post("password");
  if ($this->usermodel->login($login, $password)) {
      $this->session->set_userdata("user", $login);
      redirect("dashboard", "refresh");
  } else {
      redirect("fail", "refresh");
  }
}

public function logout() {
  $this->session->unset_userdata('user');
  session_destroy();
  redirect('index', 'refresh');
}

}
  • 写回答

1条回答 默认 最新

  • duancan9815 2017-08-14 09:46
    关注

    you must check if user is logged in at beginning of each function which must have user privileges to view

    if (!$this->session->userdata("user")) {
      redirect("auth/login", "refresh");
    }
    

    I am assuming that your view function can only be viewed if a user is logged in, change

    private function view($page, $data=false) {
        if($page == "auth/login" ){
            $this->load->view("auth/header_auth.php");
        }else{
          $this->load->view("header.php");
      }
    
       $this->load->view($page, $data);
       $this->load->view("footer.php");
     }
    

    to this

    private function view($page, $data=false) {
    if (!$this->session->userdata("user")) {
        redirect("auth/login", "refresh");
    }
        if($page == "auth/login" ){
            $this->load->view("auth/header_auth.php");
        }else{
          $this->load->view("header.php");
      }
    
       $this->load->view($page, $data);
       $this->load->view("footer.php");
     }
    

    Update

    also your dashboard can be viewed by everyone, to make it viewable only by logged in user do this:

    public function dashboard() {
      if (!$this->session->userdata("user")) {
        redirect("auth/login", "refresh");
      }
      $this->view("auth/dashboard");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度