Cookie不能与登录系统CodeIgniter一起使用 [英] Cookie not working with Login System CodeIgniter

查看:112
本文介绍了Cookie不能与登录系统CodeIgniter一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter实现了一个基本的登录系统。我使用 sessions 库来控制对仅限成员的访问。我可以登录并查看此区域没有问题。但是,当我删除我的cookie和刷新成员只有部分页面,我仍然可以看到的内容。我不向用户显示登录消息。



我不知道为什么会发生这种情况。有任何想法?



这是我的Site.php控制器

 网站扩展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'您需要登录才能访问此页面。 < a href =../ login>登录< / a>';
die();
}

}
}



这是我的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){//如果验证的凭​​据
$ data = array(
'username'=> $ this-> input-> post('username'
'is_logged_in'=> true
);

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

else {//如果没有验证重新加载登录表单
$ this-> index();
}
}

函数注册(){
$ data ['main_content'] ='signup_form';
$ this-> load-> view('includes / template',$ data);

}

function create_member(){
$ this-> load-> library('form_validation');
//字段名,错误消息,验证规则
$ this-> form_validation-> set_rules('first_name','Name','trim | required');
$ this-> form_validation-> set_rules('last_name','姓','trim | required');
$ this-> form_validation-> set_rules('email_address','电子邮件地址','trim |必需| 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 {
//在db中创建一个新行
$ this-> load-> model('membership_model');
if($ query = $ this-> membership_model-> create_member()){
//输入的信息
$ data ['main_content'] ='signup_successful';
$ this-> load-> view('includes / template',$ data);
}
else {
$ this-> load-> view('signup_form');
}
}



}


b $ b

}



这是在会话不存在时应该执行的代码。它在 site.php 文件中找到。

  if isset($ is_logged_in)|| $ is_logged_in!= true){
echo'您需要登录才能访问此页面。 < a href =../ login>登录< / a>';
die();

任何帮助都非常感谢!



也许是因为它需要两个下划线,而不是1:)

p>

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


I have implemented a basic login system using CodeIgniter. I am using the sessions library to control access to a members only section. 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. 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

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

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.

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 :)

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

这篇关于Cookie不能与登录系统CodeIgniter一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆