简体   繁体   English

Cookie无法与登录系统CodeIgniter一起使用

[英]Cookie not working with Login System CodeIgniter

I have implemented a basic login system using CodeIgniter. 我已经使用CodeIgniter实现了基本的登录系统。 I am using the sessions library to control access to a members only section. 我正在使用sessions库来控制对“仅限会员”部分的访问。 I can log in and view this area no problem. 我可以登录并查看该区域没有问题。 However when I delete my cookies and refresh the members only section page I can still see the content. 但是,当我删除Cookie并刷新“仅限会员”部分页面时,仍然可以看到内容。 I am not displaying a login message to the user. 我没有向用户显示登录消息。

I don't know why this is happening. 我不知道为什么会这样。 Any ideas? 有任何想法吗?

This is my Site.php controller 这是我的Site.php控制器

class Site extends CI_Controller{

function _construct(){
    parent::_construct();
    $this->is_logged_in();
}

function members_area(){
    $this->load->view('members_area');
}

function is_logged_in(){
    $is_logged_in = $this->session->userdata('is_logged_in');
    if(!isset($is_logged_in) || $is_logged_in != true){
        echo 'You need to login to access this page. <a href="../login">Login</a>';
        die();
}

} } }}

This is my login.php Controller 这是我的login.php控制器

class Login extends CI_Controller{
function index(){
    $data['main_content'] = 'login_form';
    $this->load->view('includes/template', $data);
}


function validate_credentials(){
    $this->load->model('membership_model');
    $query = $this->membership_model->validate();
    if($query){//if credentials validated
        $data = array(
            'username' => $this->input->post('username'),
            'is_logged_in' => true
            );

        $this->session->set_userdata($data);
        redirect('site/members_area');
    }

    else{//If not validated re load login form
        $this->index();
    }
}

function signup(){
    $data['main_content'] = 'signup_form';
    $this->load->view('includes/template', $data);

}

function create_member(){
    $this->load->library('form_validation');
    //field name, error message, validation rules
    $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
    $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
    $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');

    if($this->form_validation->run() == FALSE){
        $this->signup();
    }
    else{
        //create a new row in db 
        $this->load->model('membership_model');
        if($query = $this->membership_model->create_member()){
            //Info entered
            $data['main_content'] = 'signup_successful';
            $this->load->view('includes/template', $data);
        }
        else{
            $this->load->view('signup_form');
        }
    }



}

} }

This is the code that should be executed if a session does not exist. 如果会话不存在,这是应该执行的代码。 It is found in the site.php file. site.php文件中找到它。

if(!isset($is_logged_in) || $is_logged_in != true){
        echo 'You need to login to access this page. <a href="../login">Login</a>';
        die();

Any help is much appreciated! 任何帮助深表感谢!

Looks like your constructor isn't being called. 看起来您的构造函数没有被调用。

Perhaps it is because it needs two underscores, not 1 :) 也许是因为它需要两个下划线,而不是1 :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM