无法在codeigniter中销毁会话

时间:2015-07-31 09:22:38

标签: php codeigniter session

我有2个控制器(AuthController.php,AdminController.php)和一个登录/注册系统的母版页。 sess_destroy()在AuthController中,我使用构造方法检查用户是否登录,如果登录则重定向到(AdminController / home)。我在主页面中放置一个锚点来注销(或销毁会话)。 问题是当我点击退出时会话没有破坏。页面重定向到(Authcontroller / login)并再次重定向到(AdminController / home)。

AuthController.php:

    <div class="form-group has-feedback" ng-class="{'has-error' : config.workingDir.$invalid, 'has-success' : config.workingDir.$valid}">
        <label for="workingDir" class="control-label">Working directory</label>
        <input id="workingDir" class="has-feedback form-control" ng-model="configuration.workingDir" type="text" name="workingDir" ui-validate="'existingDir($value)'" required>
    </div>
    <div class="form-group has-feedback" ng-class="{'has-error' : config.robotFile.$invalid, 'has-success' : config.robotFile.$valid}">
        <label for="robotFile" class="control-label">Robot description file</label>
        <input id="robotFile" class="has-feedback form-control" ng-model="configuration.robotFile" type="text" name="robotFile" ui-validate="'existingFile($value)'" required>
    </div>

}

AdminController.php:

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

class AuthController extends CI_Controller{

public function __construct()
{
    parent::__construct();
    if($this->session->userdata('is_logged_in')){
        redirect('AdminController/home', 'refresh');
    }
}

public function index()
{
    $this->login();
}

public function login()
{
    $data = [
        'title'=>'Welcome to Admin Panel',
        'include'=>'admin/login'
    ];
    $this->load->view('admin/master', $data);
}

public function login_validation()
{
    $data = [
        'title' => 'Error Login'
    ];

    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean|callback_validate_credential');
    $this->form_validation->set_rules('password', 'Password', 'required|trim');

    if($this->form_validation->run()){
        $data = [
            'username' => $this->input->post('username'),
            'is_logged_in' => 1
        ];
        $this->session->set_userdata($data);
        redirect('AdminController/home');
    }else{
        $this->load->view('admin/login', $data);
    }
}

public function register()
{
    $data = [
        'title' => 'Create New Account',
        'include' => 'admin/register'
    ];
    $this->load->view('admin/master', $data);
}

public function registration_validation()
{
    $data = [
        'title' => 'Error Creating Account',
    ];
    $this->load->library('form_validation');

    $this->form_validation->set_rules('name', 'Full Name', 'required|trim');
    $this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[users.username]');
    $this->form_validation->set_rules('password', 'Password', 'required|trim');
    $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|trim|matches[password]');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[users.email]');

    $this->form_validation->set_message('is_unique', 'The username or password already exist!');

    if($this->form_validation->run()){
        $this->load->model('users','',true);
        $this->users->addUser();
        redirect('AuthController/index');
    }else{
        $this->load->view('admin/register', $data);
    }

}

public function validate_credential()
{
    $this->load->model('users');
    if($this->users->can_log_in()){
        return true;
    }else{
        $this->form_validation->set_message('validate_credential', 'The username or password not exist');
        return false;
    }
}

public function logout()
{
    $this->session->sess_destroy();
    redirect('AuthController/login', 'refresh');
}

}

master.php:

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

class AdminController extends CI_Controller{

public function index()
{
    $this->home();
}

public function home()
{
    $data = [
        'title' => 'Welcome to Admin Panel',
        'include' => 'admin/home'
    ];
    $this->load->view('admin/master', $data);
    var_dump($this->session->userdata());
}

4 个答案:

答案 0 :(得分:0)

你可以试试这个

    public function logout()
    {
        $this->session->sess_destroy();
       // null the session (just in case):
       $this->session->set_userdata(array('is_logged_in' => ''));

        redirect('AuthController/login', 'refresh');
    }

答案 1 :(得分:0)

在退出功能

中尝试此操作
  $array_items = array('username' => '');
  $this->session->unset_userdata($array_items);

答案 2 :(得分:0)

谢谢你们。我刚刚通过AdminController中的set logout方法解决了这个问题。

答案 3 :(得分:0)

您检查自己的配置文件

$config['sess_regenerate_destroy'] = TRUE;