按下退出按钮并禁用返回浏览器按钮


Codeigniter pressing logout button and disable the back browser button

你好,我正在使用CodeIgniter框架,我在注销后有一个问题,会话已经被销毁并重定向到登录表单,重定向到登录表单后,浏览器返回按钮可以回到仪表板,但由于会话已经被销毁,因此存在错误。所有我想要的是禁用浏览器的后退按钮或任何东西,我以前不能加载。我读过关于这个问题的其他帖子,并尝试了他们的解决方案,但它不起作用。我已经根据我在构造函数的其他帖子中读到的内容粘贴了这段代码。

The code that I've seen from the post and posted in my constructor :
 header("cache-Control: no-store, no-cache, must-revalidate");
        header("cache-Control: post-check=0, pre-check=0", false);
        // HTTP/1.0
        header("Pragma: no-cache");
        // Date in the past
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        // always modified
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 

这是我的注销:

 $sess_array = array(
        'username' => ''
        );
        $this->session->unset_userdata('logged_in', $sess_array);
        $this->session->sess_destroy();
        redirect('auth', 'refresh');

here is from my dashboard:

    <li>                                 
<a href="<?= base_url('auth/logout') ?>"><i3 class="glyphicon glyphicon-off"></i3> Logout</a>
   </li>

我想实现这个选项,但效果不太好。所以我在这上面实现了新的逻辑。

简单地检查是否在每个主方法中设置会话。下面的代码可以帮助您

In logout(define In controller)

function __construct()
{
    parent::__construct();
    ob_start(); # add this
}
public function logout()
{
    $this->load->driver('cache');
    $this->session->sess_destroy();
    $this->cache->clean();
    ob_clean();
    redirect('home'); # Login form or some other page         
}
在仪表板

(函数)

public function home()
{
    $logged_in = $this->session->userdata('logged_in');
    if($logged_in != TRUE || empty($logged_in))
    {
        #user not logged in
        $this->session->set_flashdata('error', 'Session has Expired');
        redirect('user_logging'); # Login view
    }
    else
    {
        #user Logged in
        $this->load->view("viewname",$data);
    }
}

In Login(function)

$session = array(
    'username'  => $name,
    'logged_in' => TRUE
);
$this->session->set_userdata($session);

您正在尝试解决错误的问题:如果在注销后返回导致一个充满错误的页面,直接进入该页面将导致相同的问题。

这种情况不应该发生,相反,当有人试图打开一个未登录时不应该打开的页面时,应该将访问者引导到登录页面。

除此之外,你不应该扰乱用户的浏览器体验。所以即使你可以,你也不应该禁用后退按钮。即使可以,也可以通过禁用javascript(例如…)轻松规避。

首先,设置userdata会话值,当您成功登录到管理面板时使用这一行在您从

登录时完成的所有验证之后

$ this ->会话> set_userdata (admin_id, admin_id美元);

你可以做一件事来避免直接登录你的用户面板或管理面板的页面,只要在你的管理控制器中创建一个构造函数,像这样

public function __construct()
{
    parent::__construct();
    if(! $this->session->userdata('admin_id')){
        return redirect('login_controller');//your login controller which have login page as view in index function//
    }
} 

,并像这样使用注销功能:

public function logout()
{
    $this->session->unset_userdata('admin_id');
    return redirect('login_controller');
}

通过这种方式,当你从管理面板注销并尝试使用后退按钮或尝试使用直接调用任何管理面板页面或功能,它不会打开,它适用于我,仍然使用此代码在用户和管理面板制作

希望这对你有帮助

您的登录功能

function login() {
     $query = $this->login_model->get_user($username, $password);
     foreach ($query as $row) {
          $username = $row->username;
     }
     $session_array = array(
          'username' => $username,
          'logged_in' => TRUE
     );
     $this->session->set_userdata('logged_in', $session_array);
}

您的登出功能

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

在仪表板或家用控制器

function index() {
    if ($this->session->userdata('logged_in') !== FALSE && ($this->session->userdata['logged_in']['login_type'] == "" ))
    {
         // Your codes 
    } else {
       redirect('login/logout');
    }
}
you can use the construct method at the beginning of your admin controller, inside construct add the condition it will work in every function as long as the controller is admin,
public function __construct()
{
    parent::__construct();
    if(! $this->session->userdata('admin_id')){
        return redirect('login_controller');//your login controller which have login page as view in index function//
    }
} 

but as you move on deep into your web application and browse through several controllers and methods, you will encounter "page not found page" if you log out somewhere in the middle of the page with a controller other than admin,
To solve this problem add the construct method like below code in all the controllers construct at the beginning and then you will be able to logout from anywhere with session destroy,

public function __construct()
{
    parent::__construct();
    if(! $this->session->userdata('admin_id')){
        return redirect('login_controller');//your login controller which have login page as view in index function//
    }
}