Cookies not beeing set, codeigniter

2019-08-28 19:03发布

问题:

I'm trying out cookies for first time in CI. Cant get it to work. Here's my code:

class Site extends CI_Controller {

public function index(){
    $this->load->view('input_view');
    $this->load->helper('cookie');
    $this->test();
}

public function test(){
    $cookie = array(
        'name'   => 'Test',
        'value'  => 'The Value',
        'expire' => '86500',
        //'domain' => '.some-domain.com',
        'path'   => '/',
        'prefix' => 'myprefix_',
        'secure' => TRUE
    );

    $this->input->set_cookie($cookie);
    var_dump(get_cookie('Test'));
}

var_dump... returns false. I understand it does this the first time but should'nt it be true after reloading the page? I can't see any "Test" cookie in my web tools in Chrome either. Do I need the "domain" row? I googled some and if I understood it was optional? If it isn't, what am i suppose to write there? Localhost? In my config file i have this settings for cookies

$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;

回答1:

I'm guessing you're not running https for development.

The secure flag on your cookie forces the cookie to be sent over HTTPS only;

Secure:

If this cookie can ONLY be sent over HTTPS (SSL), set this value to true.

Set it to false, and the cookie should work.



标签: codeigniter