我試圖限制用戶未登錄的頁面訪問。奇怪的是,當試圖訪問未經授權的用戶時,網頁顯示頁面對於受限訪問(使用「未經授權,請登錄」的消息,並在同一頁面上,它只加載成員頁面)。Codeigniter會話認證仍允許加載受限制的頁面
對於現場控制器:
class Site extends CI_Controller {
function __construct(){
parent::__construct();
$isLogged=$this->session->userdata('logged');
if($isLogged!='logged' || !isset($isLogged)){
$data['content']='denied';
$this->load->view('include/template', $data);
}
}
function members(){
$data['content']='memberArea';
$this->load->view('include/template', $data);
}
登錄表單:
class Login extends CI_Controller {
function index() {
$data['content']='loginForm';
$this->load->view('include/template', $data);
}
function validateMember(){
// load the model for the login authentification
$this->load->model('loginModel');
//query should also contain the user details
$query = $this->loginModel->authenticate();
//if it returns something:
if($query){
foreach ($query as $row){
//set user data to be passed
$details=array(
'username'=>$row->user_name,
'id'=>$row->id,
'logged'=>'logged',);
}
//set session
$this->session->set_userdata($details);
redirect('site/members');
}
else{
$this->index();
}
}
的登錄的模式是:
class LoginModel extends CI_Model {
function authenticate(){
//select active fields
$this->db->select('id, user_name, password');
// query to select from table where conditions
$query = $this->db->get_where('login', array(
'user_name'=>$this->input->post('username'),
'password'=>$this->input->post('password'),),
1);
//if it finds something...must ask if i need to manually close the database with $this->db->close();
if($query->num_rows()==1){
foreach($query->result() as $row){
$data[]=$row;
}
return $data;
}
else {return false;}
}
}
我的測試表明,現場繼續調查l即使構造函數失敗,其他函數也是如此。會話確實包含數據。如果我使用die()或exit(),則網頁加載空白。提前謝謝了!
PS:只有<p>
在他們的意見,沒有什麼幻想。
嗨,如果你想添加自己的答案,請做 - 但在答案框中這樣做,而不是作爲問題的更新。不要在標題中添加「已解決」,請勾選您最喜歡的答案(如果您願意,也可以選擇自己的答案)。我已經回到以前的版本,但你可以[從這裏複製你的編輯](http://stackoverflow.com/posts/15599079/revisions)。 – halfer 2013-03-24 14:27:45
哦,對不起。我會選擇我最喜歡的答案。沒有進攻意圖的球員。 – 2013-03-24 14:56:40