【问题标题】:I can't use cookie in Codeigniter我不能在 Codeigniter 中使用 cookie
【发布时间】:2011-10-15 18:25:42
【问题描述】:

我想在 Codeigniter 中使用 cookie 保持登录状态,但我无法在 Codeigniter 中设置 cookie。这是我在 Controller 中的代码。当我单击登录表单中的提交按钮时,它会调用 chk_login() 函数来检查数据库中的用户名和密码。之后,它会转到这一行 echo "You are not log in" 而已。如何在 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
    }  
}

}
?>

我尝试使用此代码进行测试,它显示 bool(false)

$cookie = array(
          'name'   => 'test_cookie',
          'value'  => 'test',
          'domain' => '/',
          'secure' => TRUE
          );

$this->input->set_cookie($cookie); 

var_dump($this->input->cookie('name'));

【问题讨论】:

  • 您在哪里实际设置了您要验证的 cookie?
  • 我想我会尝试使用 JavaScript 验证表单。我应该如何处理 cookie。
  • 字符串“$this->input->set_cookie()”在哪里?
  • $this->input->set_cookie() 在函数 chk_login() 的 else{ } 中。 $this-&gt;input-&gt;set_cookie(array( 'login'=&gt;TRUE, 'username'=&gt;"aaa", 'password'=&gt;"123", 'status'=&gt;"user"));
  • 试试这个:redirect('login/goto_test', 'refresh');

标签: php codeigniter cookies


【解决方案1】:

现在我知道了。我不能这样设置cookie。我有两种设置 cookie 的方法。

1.

$cookies = array(
        array(
          'name'   => 'login',
          'value'  => true,
          'domain' => '/',
          'secure' => true
    ),
        array(
          'name'   => 'username',
          'value'  => 'aaa',
          'domain' => '/',
          'secure' => true
    ),
        array(
          'name'   => 'password',
          'value'  => '123',
          'domain' => '/',
          'secure' => true
    )
);

foreach($cookie in $cookies){
        $this->input->set_cookie($cookie); 
}

2.

setcookie('login', true);
setcookie('username','aaa');
setcookie('password','123');

cookie不能这样设置。

$cookie = array(
          'login'   => TRUE,
          'username'  => 'aaa',
          'password' => '123'
          );

$this->input->set_cookie($cookie); 

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 2012-04-05
    • 2011-04-20
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2012-02-21
    相关资源
    最近更新 更多