如何修复CodeIgniter-3中没有工作的会话
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修复CodeIgniter-3中没有工作的会话相关的知识,希望对你有一定的参考价值。
我正在使用CodeIgniter 3.1.10并尝试使用我找到的教程进行会话登录。我认为会话不起作用,因为当我单击后退按钮时,即使我已登录,页面也会加载回登录页面。
以下是名为login_view的视图中的表单:
<form action="<?php echo base_url('index.php/Login_controller/login')?>" method="post">
<input type="text" placeholder="Username" name="username" id="username"/>
<input type="password" placeholder="password" name="password" id="password" />
<div class="text-center">
<input type="submit" name="submit" class="button" value="LOG IN" style="border-radius: 4px;border: none;">
<a href="signup.html" target="_blank"><div class="button">Create an account</div></a>
</div>
<?php
if($this->session->flashdata('error')){
?>
<div class="alert alert-danger text-center" style="margin-top:20px;">
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php
}
?>
</form>
Login_controller.php:
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('login_model');
}
public function index(){
//load session library
$this->load->library('session');
//restrict users to go back to login if session has been set
if($this->session->userdata('user')){
redirect('ask_controller/index','refresh');
}
else{
$this->load->view('login_view');
}
}
public function login(){
//load session library
$this->load->library('session');
$username = $_POST['username'];
$password = $_POST['password'];
$data = $this->login_model->login($username, $password);
if($data){
$this->session->set_userdata('user', $data);
redirect('ask_controller');
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
public function home(){
//load session library
$this->load->library('session');
//restrict users to go to home if not logged in
if($this->session->userdata('user')){
$this->load->view('ask_controller');
}
else{
redirect('/');
}
}
public function logout(){
//load session library
$this->load->library('session');
$this->session->unset_userdata('user');
redirect('/');
}
Login_model.php:
public function login($username, $password){
$query = $this->db->get_where('user', array('username'=>$username, 'password'=>$password));
return $query->row_array();
}
来自config.php:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
我还在自动加载库中添加了“session”。这里使用的表名是'user'。
即使我点击“返回”,它也应该加载我登录后输入的页面。另外,登录后我收到此错误。
答案
我跳这对你有所帮助
public function login(){
//load session library
$this->load->library('session');
$username = $_POST['username'];
$password = $_POST['password'];
$data = $this->login_model->login($username, $password);
if($data){
$this->session->set_userdata('user', $data);
redirect('ask_controller');
}
else{
$this->session->set_flashdata('error','Invalid login. User not found');
header('location:'.base_url().$this->index());
}
}
你需要更换线路
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found');
至
$this->session->set_flashdata('error','Invalid login. User not found');
header('location:'.base_url().$this->index());
以上是关于如何修复CodeIgniter-3中没有工作的会话的主要内容,如果未能解决你的问题,请参考以下文章