codeigniter sess_destroy()无法正常工作,我在做什么错? [英] codeigniter sess_destroy() not working properly,what m i doing wrong?

查看:90
本文介绍了codeigniter sess_destroy()无法正常工作,我在做什么错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Codeigniter的新手。我正在使用登录表单以管理员身份登录。当管理员使用正确的用户名和密码登录时,他/他将使用会话变量定向到主页。然后,如果他单击注销按钮,则该会话将被破坏并重定向用户以登录页面即登录表单页面。

I am a newbie in codeigniter. I am using an an login form to login as an admin. When the admin logs in with the correct user name and password s/he is directed to the home page with a session variable.and then if he clicks the log out button the session is supposed to be destroyed and redirect the user to log in page i.e log in form page.

第一个控制器是管理员:

The 1st controller is admin:

<?php
class Admin extends CI_Controller
{
    function index()
    {
        $data['main_content'] = 'admin/log_in';
        $this -> load -> view('includes/admin/admin_template', $data);
    }
    function log_in()
    {
        $this->load->model('admin_model');
        $query = $this -> admin_model -> validate();
        if ($query)// if the user's credentials validated...
        {
            $data = array('user_name' => $this -> input -> post('user_name'), 'is_logged_in' => true);
            $this -> session -> set_userdata($data);
            redirect('admin/home/admin_home');
        } else// incorrect username or password
        {
            $this -> index();
        }
    }
    function log_out()
    {
        $this->session->sess_destroy();
        redirect('/admin/admin','refresh');
    }
}

第二个控制器是家庭控制器:

The second controller is the home controller:

<?php
class Home extends CI_Controller
{
    function __construct()
    {
        parent:: __construct();
        $this->is_logged_in();
    }
    function is_logged_in() 
    {
        $is_logged_in = $this -> session -> userdata('is_logged_in');
        if (!isset($is_logged_in) || $is_logged_in != true)
        {
            $this -> load -> view('admin/forbidden');
        }
    }
    function admin_home()
    {
        $data['main_content'] = 'home_view';
        $this->load->view('admin/home_view');
    }
}

模型为admin_model:

The model is admin_model:

<?php
class Admin_model extends CI_Model
{
    function __construct()
    {
        parent:: __construct();
    }
    function validate()
    {
            $this->db->where('user_name',$this->input->post('user_name'));
            $this->db->where('password', $this->input->post('password'));
            $query = $this->db->get('user');
            if($query->num_rows==1)
            {
                return true;
            }
    }
}

现在,假设用户退出并销毁会话,但是如果我单击浏览器的后退按钮,我可以返回原本应该不是的页面,并且会话也不会销毁。
请告诉我我在这里做错了。我正在使用codeigniter 2.1.0。

Now, it supposed the user to logout and destroy the session, but if I click the back button of my browser I can get page back which was supposed not to be and the session is not destroyed. please tell me what I am doing wrong here. I am using codeigniter 2.1.0.

推荐答案

经过所有麻烦并在各个地方进行搜索后,我终于找到了一个合适的解决方案解决此问题的方法。问题到来是因为浏览器正在显示缓存的页面。不是会话在引起问题,并且工作正常。
是解决方案:home控制器中的
添加一个功能来清除缓存,并在构造函数中调用它可以解决问题:)
这是具有解决方案的home控制器: / p>

after going through all the troubles and searching in various places i have finally found a proper solution to this question.the problem arrived because the browser was showing the cached pages.it was not the session that was creating the problem and it was working properly. here is the solution: in the home controller adding a function to clear the cache and calling it in the constructor function does the trick :) here is the home controller with the solution:

<?php
class Home extends CI_Controller
{
    function __construct()
    {
        parent:: __construct();
        $this->is_logged_in();
        $this->clear_cache();
    }
    function is_logged_in() 
    {

        if (!$this->session->userdata('is_logged_in'))
        {
            redirect('/admin/admin');
        }
    }
    function clear_cache()
    {
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
    }
    function admin_home()
    {
        $data['main_content'] = 'home_view';
        $this->load->view('admin/home_view');
    }
}

现在,感谢您访问此链接 代码点火器中的注销功能,这是我找到解决方案的地方,它可以完美地工作:)

now thanks goes to this link " logout feature in code igniter ",here is where i have found the solution and it works perfectly :)

这篇关于codeigniter sess_destroy()无法正常工作,我在做什么错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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