【问题标题】:Codeigniter session authentification still allows restricted page to loadCodeigniter 会话身份验证仍然允许加载受限页面
【发布时间】:2013-03-14 00:08:31
【问题描述】:

如果用户未登录,我正在尝试限制来自页面的访问。奇怪的是,当未经授权的用户尝试访问该网页时,会显示受限访问的页面(带有“未经授权,请登录”消息,并在同一页面上加载仅限会员的页面)。

对于站点控制器:

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;}
}

}

我的测试表明,即使构造函数失败,站点也会继续调用其他函数。会话确实包含数据。如果我使用 die() 或 exit() 网页加载空白。非常感谢!

PS:视图中只有<p>,没什么特别的。

【问题讨论】:

  • 您好,如果您想添加自己的答案,请添加 - 但请在答案框中添加,而不是作为问题的更新。与其在标题中添加“已解决”,不如勾选您最喜欢的答案(如果您愿意,也可以选择您自己的答案)。我已将问题回滚到之前的版本,但您可以copy your edit from here
  • 哦,对不起。然后我会选择我最喜欢的答案。没有冒犯的意思。

标签: php codeigniter


【解决方案1】:

我可以看到这个问题的两种解决方案。

  1. 重定向到另一个不检查身份验证的页面。您可以使用 URL 帮助程序中的redirect(<url>);

  2. __construct() 中使用exit() 并刷新缓冲输出。当您调用 $this->load->view() 时,数据将发送到 CodeIgniter 中名为 output 的缓冲区。您可以通过执行以下操作来写入该缓冲区:

    if($isLogged!='logged' || !isset($isLogged)){
    
        $data['content']='denied';
        $this->load->view('include/template', $data);
    
        // Write the output.
        echo $this->output->get_output();  
    
        // Stop the execution of the script.
        exit();
    }
    

    或者你可以绕过输出缓冲区:

    if($isLogged!='logged' || !isset($isLogged)){
    
        $data['content']='denied';          
    
        // Writes the content instead of sending it to the buffer.  
        echo $this->load->view('include/template', $data, true);  
    
        // Stop the execution of the script.
        exit();
    } 
    

选择你想要的。

【讨论】:

    【解决方案2】:

    构造方法没有“失败”,if 语句被简单地执行(因为条件为真)并且没有理由阻止其他方法被执行。

    在构造方法中,在if 块的末尾,您可以插入exit 语句,这样页面可能会按照您的预期运行。

    请看这里http://php.net/manual/en/function.exit.php

    【讨论】:

    • 好吧,if 语句在加载受限访问页面内容时被执行。它只是继续加载另一个页面......
    • 是的对不起你是对的,但问题仍然是你没有阻止其他方法与构造函数一起执行,你需要一个退出语句,现在我编辑我的答案。
    • 感谢您的建议,但正如我已经说过的,我尝试在 if 中放置 die 或 exit 或 return 或 break 语句以停止执行其他方法。问题是我不太明白为什么如果构造方法重定向到一个页面,它会继续加载另一个页面。在同一页面中。好像构造函数方法连接了两个函数。这很奇怪!
    • 因为是构造函数,所以每次创建实例都会执行,不会阻止其他方法执行,看这个视频,从7'30开始看tutorialcodeigniter.com/beginners/creating-controller.html
    • 非常感谢,我会看这个的。简短更新:似乎如果我使用重定向,它会将我重定向到登录页面。也许答案是重定向脚本应该位于另一个控制器中。我会尝试后更新
    【解决方案3】:

    您正在测试是否有查询,而不是查询是否实际返回任何内容。检查 num 行并确保它们等于 1,大于 0 对登录检查是不好的做法。

    if($query->num_rows==1){
    

    PS。也发布您的模型代码,可能存在错误并且即使它不应该返回结果。

    【讨论】:

    • 感谢您的建议,但是在将数据传递给控制器​​之前,会在模型中检查 num_rows。我无法在控制器中检查它,因为它说非对象。
    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 2020-08-08
    • 2023-04-09
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多