我正在使用CodeIgniter 3.1.8和 Twitter Bootstrap 4 開發在線報紙/博客應用程式。當然,該應用程式具有登錄和注冊系統。
我目前正在為登錄表單添加“記住我”功能。
在application\views\auth\login.php我有:
<?php echo form_open(base_url('login/login')); ?>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="text" name="email" id="email" value="<?php if(isset($_COOKIE['userEmail'])) { echo $_COOKIE['userEmail']; } ?>" hljs-string">" placeholder="Email">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div hljs-meta"><?php if(form_error('password')) echo 'has-error';?>">
<input type="password" name="password" id="password" value="<?php if(isset($_COOKIE['userPassword'])) { echo $_COOKIE['userPassword']; } ?>" hljs-string">" placeholder="Password">
<?php if(form_error('password')) echo form_error('password'); ?>
</div>
<div hljs-string">">
<input type="checkbox" name="remember_me" value="true" id="remember_me">
<span hljs-string">">Remember me</span>
</div>
<div hljs-string">">
<input type="submit" value="Login" hljs-string">">
</div>
<?php echo form_close(); ?>
在控制器 ( application\controllers\Login.php) 中,我有:
public function login() {
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_error_delimiters('<p >', '</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 we find a user
if ($current_user) {
// If the user found is active
if ($current_user->active == 1) {
$this->session->set_userdata(
array(
'user_id' => $current_user->id,
'user_email' => $current_user->email,
'user_avatar' => $current_user->avatar,
'user_first_name' => $current_user->first_name,
'user_is_admin' => $current_user->is_admin,
'user_active' => $current_user->active,
'is_logged_in' => TRUE
)
);
// Remember me
if (!empty($this->input->post('remember_me'))) {
setcookie ('userEmail', $email, time() (7 * 24 * 3600));
setcookie ('userPassword', $password, time() (7 * 24 * 3600));
} else {
setcookie ('userEmail', '');
setcookie ('userPassword','');
}
// After login, display flash message
$this->session->set_flashdata('user_signin', 'You have signed in');
//and redirect to the posts page
redirect('/dashboard');
} else {
// If the user found is NOT active
$this->session->set_flashdata("login_failure_activation", "Your account has not been activated yet.");
redirect('login');
}
} else {
// If we do NOT find a user
$this->session->set_flashdata("login_failure_incorrect", "Incorrect email or password.");
redirect('login');
}
}
else {
$this->index();
}
}
問題
由于我一直無法發現的原因,即使未remember_me選中該復選框,登錄憑據仍會被記住。
更新
我去了application\config\config.php并替換了$config['sess_expiration'] = 7200寬度:
$config['sess_expiration'] = 0;
結果,記憶不再發生。
我的錯誤在哪里?
uj5u.com熱心網友回復:
在您的身份驗證檢查腳本中也檢查 cookie 過期時間,一旦過期清除會話變數。
uj5u.com熱心網友回復:
你在用 Chrome 測驗嗎?我認為 Chrome 無論如何都會記住登錄狀態(關閉并重新打開瀏覽器后您仍然處于登錄狀態),而其他瀏覽器,如 Firefox,將在重新打開時丟失 cookie。
您也可以在這里測驗:https : //demo.fleio.com/
uj5u.com熱心網友回復:
setcookie('userEmail', $email, 0, "/");
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/328580.html
