2
我想在Codeigniter中使用Cookie保持登錄狀態,但我無法在Codeigniter中設置Cookie。這是我在Controller中的代碼。當我在登錄表單中單擊提交按鈕時,它會調用chk_login()函數來檢查數據庫中的用戶名和密碼。之後,它將轉到此行回顯「您沒有登錄」而已。如何在Codeigniter中使用cookie。我不能在Codeigniter中使用Cookie
<?php
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('cookie');
}
function index()
{
redirect('login/form_login');
}
function form_login()
{
$data['title'] = 'Login';
$this->load->view('form_login_view', $data);
}
function goto_test()
{
if($this->input->cookie('login')){
echo "You are logged in";
}
else{
echo "You are not log in"; // It show string in this line every time. that mean cookie not set yet. how to use cookie in Codeigniter.
}
}
function chk_login()
{
$this->load->model('login_model', 'login');
$this->login->username = $this->input->post('username');
$this->login->password = $this->input->post('password');
$user_login = $this->login->get_by_user_pass();
if($user_login==null){
$data['errors'] = 'Not found Username.<br />';
$this->load->view('form_login_view', $data);
}else{
// I set directed data in cookie but I can't get this cookie in goto_test().
$this->input->set_cookie(array(
'login'=>TRUE,
'username'=>"aaa",
'password'=>"123",
'status'=>"user"));
redirect('login/goto_test'); // Go to function goto_test in this class
}
}
}
?>
我嘗試使用此代碼顯示它布爾(假)測試。
$cookie = array(
'name' => 'test_cookie',
'value' => 'test',
'domain' => '/',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('name'));
你究竟在哪裏設置你的cookie,你試圖驗證? – Repox
我想我會嘗試使用JavaScript驗證表單。我應該如何處理cookie。 – user572575
字符串「$ this-> input-> set_cookie()」在哪裏? – Ivan