我正在开发 Codeigniter 3身份验证应用程序。我是CI的新手,所以遇到的问题都是“菜鸟”级别。
最新的一个是:如果用户已登录,即使他/她被重定向到成员区域,登录状态也不会持续存在。导航到主页(或任何其他页面)签署用户。
用户登录后, 希望登录表单页面无法访问。
会话库全局加载。
用户模型是:
class Usermodel extends CI_Model {
public function user_login($email, $password)
{
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password));
return $query->row();
}
}
控制器如下所示:
class Signin extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('signin');
}
public function signin()
{
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run())
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->load->model('Usermodel');
$current_user = $this->Usermodel->user_login($email, $password);
if ($current_user) {
$current_user_id = $this->session->userdata('user_id', $current_user->id);
$current_user_email = $this->session->userdata('email', $current_user->email);
redirect('home');
} else {
$this->session->set_flashdata("signin_failure", "Incorrect email or password");
$this->load->view('signin');
}
}
else
{
$this->load->view('signin');
}
}
}
登录表单视图:
<?php echo form_open('Signin/signin'); ?>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<?php echo form_input(array(
'name' => 'email',
'id' => 'email',
'class' => 'form-control',
'autocomplete' => 'off',
'placeholder' => 'Email address'
));
if(form_error('email')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('password')) echo 'has-error';?>">
<?php echo form_password(array(
'name' => 'password',
'id' => 'password',
'class' => 'form-control',
'autocomplete' => 'off',
'placeholder' => 'Password'
));
if(form_error('password')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('password'); ?>
</div>
<div class="form-group">
<?php echo form_submit(array(
'name' => 'login',
'value' => 'Login',
'class' => 'btn btn-success btn-block'
)); ?>
</div>
<?php echo form_close(); ?>
为什么没有保留登录状态?谢谢!
UPDATE 用户帮助:
function is_logged_in() {
$CI =& get_instance();
$user = $CI->session->userdata('user_data');
return isset($user);
}
导航条码:
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<?php if(is_logged_in()) : ?>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="glyphicon glyphicon-user"></span> Hello, user
<span class="caret"></span></a>
</a>
<ul class="dropdown-menu">
<li><a href="#">My contacts</a></li>
<li><a href="#">All contacts</a></li>
<li><a href="<?php echo base_url('index.php/Signin/signout'); ?>" title="Signout">Signout</a></li>
</ul>
</li>
<?php else: ?>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Register</a></li>
<?php endif;?>
</ul>
</div>
答案 0 :(得分:0)
您需要先设置userdata。
变化
$current_user_id = $this->session->userdata('user_id', $current_user->id);
$current_user_email = $this->session->userdata('email', $current_user->email);
到
$current_user_id = $this->session->set_userdata('user_id', $current_user->id);
$current_user_email = $this->session->set_userdata('email', $current_user->email);
答案 1 :(得分:0)
// if found in table, then save it session // so that it can be accessible from elsewhere if ($current_user) { $this->session->set_userdata( array( 'user_id' => $current_user->id 'email'=> $current_user->email ) ); }
因此,在helpers
目录中,假设您使用以下代码创建文件user_helper.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
function is_logged_in() {
$CI =& get_instance();
$user = $CI->session->userdata('user_data');
return isset($user);
}
config/autoload.php
并添加您的助手,以便在所有其他控制器中轻松实现:$autoload['helper'] = array('user');
因此,在任何控制器中,您都可以轻松检查用户是否登录,如下所示
if(is_logged_in()){
// proceed authenticated
}else{
// redirect to signin page or somewhere else as per your wish
redirect('signin');
}
你说:
用户登录后,我不希望登录表单页面 访问。
所以你需要
public function signin()
{
if(is_logged_in()){
$this->session->set_flashdata('message_name', 'You already logged In');
//redirect to some function
redirect("controller/function_name");
}
// if not logged in then display form
// your other code goes here..
}
并在您重定向的视图文件中
echo $this->session->flashdata('message_name');