Php CodeIgniter-仍然可以访问成员';单击“上一步”按钮注销后的页面
我正在使用代码点火器 在我登录到该成员的帐户并单击“注销”后,我仍然可以在登录时查看该成员的页面 我点击后退按钮 问题 是否有人知道,如果用户已注销,即使单击“后退”按钮,也无法访问该成员的页面 请看下面我的代码 谢谢Php CodeIgniter-仍然可以访问成员';单击“上一步”按钮注销后的页面,php,codeigniter,Php,Codeigniter,我正在使用代码点火器 在我登录到该成员的帐户并单击“注销”后,我仍然可以在登录时查看该成员的页面 我点击后退按钮 问题 是否有人知道,如果用户已注销,即使单击“后退”按钮,也无法访问该成员的页面 请看下面我的代码 谢谢 class Site extends CI_Controller{ function index() { if(!isset($this->session->userdata['is_loggged_in']))
class Site extends CI_Controller{
function index()
{
if(!isset($this->session->userdata['is_loggged_in']))
$this->login();
else
$this->_template();
}
function login()
{
$data['main_content'] = 'login_form';
$data['page_title'] = 'Login - php';
$this->load->view('include/template' , $data);
}
function validate()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username' , 'Username' , 'trim|required' );
$this->form_validation->set_rules('password' , 'Password' , 'trim|required' );
if($this->form_validation->run() == FALSE)
{
$this->login();
}
else
{
$this->load->model('member_model' , 'member');
$query = $this->member->checklogin();
//login successful , make sure no duplicate entry during registration
if($query->num_rows() == 1)
{
$data = array(
'is_loggged_in' => TRUE ,
'username' => $this->input->post('username'),
);
$this->session->set_userdata($data);
$this->_template();
}
else
{
$data = array(
'main_content' => 'login_form' ,
'page_title' => 'Error login' ,
'page_error' => 'Invalid Username or password. '
);
$this->load->view('include/template' , $data);
}
}
}
function _template()
{
$data = array(
'main_content' => 'include/default_inc',
'page_title' => 'Welcome home ran'
);
$this->load->view('include/template' , $data);
}
function logout()
{
$this->session->sess_destroy();
redirect('site/login');
exit;
}
}
/* end of controller site */
尝试在构造函数中检查会话,而不是在索引函数中检查会话
function __constuctor()
{
parent::__construct();
$session = $this->session->userdata('is_loggged_in');
if ( !$session) {
$this->login();
}else{
$this->_template();
}
}
尝试在构造函数中检查会话,而不是在索引函数中检查会话
function __constuctor()
{
parent::__construct();
$session = $this->session->userdata('is_loggged_in');
if ( !$session) {
$this->login();
}else{
$this->_template();
}
}
基本上,你的浏览器会返回一个页面,并简单地复制它当时收到的HTML。有很多方法可以解决这个问题,我保证你可以通过小小的搜索轻松找到它们 防止这种情况的一种简单方法是在页面加载时运行ajax,检查您是否登录,如果没有登录,则更新页面。ajax将始终运行,即使使用Back 控制器/check_login.php
<?
class Check_login {
function index()
{
$bool = (bool) $this->session->userdata('is_loggged_in');
$this->output->set_content_type('application/json');
if ($bool)
$this->output->set_status_header('500');
$arr_json = array( 'need_login' => $bool);
echo json_encode($arr_json);
}
}
注意通过巧妙地使用
$This->input->is\u ajax\u request()
和'need\u login'
值,这种方法还可以为您提供一种处理死机ajax调用的简便方法。基本上,您的浏览器会返回页面,并简单地复制当时收到的HTML。有很多方法可以解决这个问题,我保证你可以通过小小的搜索轻松找到它们
防止这种情况的一种简单方法是在页面加载时运行ajax,检查您是否登录,如果没有登录,则更新页面。ajax将始终运行,即使使用Back
控制器/check_login.php
<?
class Check_login {
function index()
{
$bool = (bool) $this->session->userdata('is_loggged_in');
$this->output->set_content_type('application/json');
if ($bool)
$this->output->set_status_header('500');
$arr_json = array( 'need_login' => $bool);
echo json_encode($arr_json);
}
}
注意通过巧妙地使用
$This->input->is\u ajax\u request()
和'need\u login'
值,这种方法还可以为您提供一种处理死机ajax调用的简便方法。假设您的会话已成功销毁,但仍可以访问上一页(从浏览器的历史记录中),然后,您可以使用eachcontrollers构造函数中的代码来解决这个问题。我建议扩展核心输出库
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');
假设您的会话已成功销毁,但上一页仍被访问(来自浏览器的历史记录),那么您可以通过使用everycontrollers构造函数中的代码来解决此问题。我建议扩展核心输出库
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');
从您的代码中,您已注销,甚至html页面也被浏览器缓存,因此会发生此类问题。我提供了一个经过良好测试的解决方案。
。将以下代码放在助手中的某个位置(比如my_helper.php)
加载帮助程序并在控制器的构造函数中调用no_cache函数
public function __construct()
{
parent::__construct();
$this->load->helper('my_helper');
no_cache();
}
从您的代码中,您已注销,甚至html页面也被浏览器缓存,因此会发生此类问题。我提供了一个经过良好测试的解决方案。
。将以下代码放在助手中的某个位置(比如my_helper.php)
加载帮助程序并在控制器的构造函数中调用no_cache函数
public function __construct()
{
parent::__construct();
$this->load->helper('my_helper');
no_cache();
}
这就是逻辑
若你们禁用所有控制器中的缓存,它会工作,但它是愚蠢的。也许这会减缓你的网页负载,甚至影响你的搜索引擎优化。
因此,解决方案是始终检查每个页面中的用户状态,如果他的状态已登录,则禁用缓存,否则不执行任何操作
这是我代码的一个示例部分,我把它放在每个函数中
if ($this->session->userdata('logged_in')){
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');}
不需要在后面放else语句,只需放上其余的代码
因此,只要用户登录id并浏览您的web,就不会存储缓存。尝试注销并点击“后退”按钮。我敢打赌它会起作用这就是逻辑
若你们禁用所有控制器中的缓存,它会工作,但它是愚蠢的。也许这会减缓你的网页负载,甚至影响你的搜索引擎优化。
因此,解决方案是始终检查每个页面中的用户状态,如果他的状态已登录,则禁用缓存,否则不执行任何操作
这是我代码的一个示例部分,我把它放在每个函数中
if ($this->session->userdata('logged_in')){
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');}
不需要在后面放else语句,只需放上其余的代码
因此,只要用户登录id并浏览您的web,就不会存储缓存。尝试注销并点击“后退”按钮。我打赌它会起作用单击注销后检查您的会话是否被销毁。另外,在设置会话的每个页面中添加一个检查,否则重定向到登录验证$\u会话的状态,以确认它正在被销毁。您可以使用print\r($\u SESSION)。Portu,CI不使用$\u SESSION变量。@谢谢您的提醒,我没有机会使用CI,因此非常感谢您的输入。我原以为这只是检查会话状态的问题,但幕后似乎有一个以上的$会话变量。单击back
按钮不会重新加载页面。无论您如何注销,您仍然可以访问上一页。但是,如果刷新/重新加载页面,您将进入登录页面检查您的会话在单击“注销”后是否被销毁。另外,在设置会话的每个页面中添加一个检查,否则重定向到login验证$\u会话的状态,以确认会话是否被销毁。您可以使用print\r($\u SESSION)。Portu,CI不使用$\u SESSION变量。@谢谢您的提醒,我没有机会使用CI,因此非常感谢您的输入。我原以为这只是检查会话状态的问题,但幕后似乎有一个以上的$会话变量。单击back
按钮不会重新加载页面。无论您如何注销,您仍然可以访问上一页。但是,如果你刷新/重新加载页面,你将进入登录页面,这是我做一些更改的方式。它很好地解决了上述问题。把它投上去这是我对待s的方式