CI 3.1.8 set_cookie()不起作用?

时间:2018-05-18 06:17:27

标签: php codeigniter cookies

我知道有关此set_cookie() CI的问题太多了,但我找不到答案,Cookie没有显示在Google Cookie数据菜单中。

我知道如何阅读它,但它根本没有显示在浏览器cookie数据中。

ControllerFile.php

中的

$this->load->helper('cookie');
$cookie = array(
    'name'      => 'pre',
    'value'     => $token,
    'expire'    => 86400 * 7,
);
$this->input->set_cookie(
    $cookie['name'], $cookie['value'], $cookie['expire']
);

config.php

中的设置
$config['cookie_prefix']    = 'e_';
$config['cookie_domain']    = '.localhost';
$config['cookie_path']      = '/project.com/catch/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;

此网站项目位于 project.com 文件夹下,这就是它的样子

htdocs/
---- project.com/
-------- catch/
------------ *CI files*
--------- *files*
--------- *files*
---- anotherproject.com/
---- anotherproject.com/
---- anotherproject.com/

[::1].localhost没有Cookie数据,为什么会这样?

我已经尝试用谷歌搜索但是找不到解决方案,我已经尝试了

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

但它也不起作用。

添加log file

INFO - 2018-05-18 08:28:37 --> Config Class Initialized
INFO - 2018-05-18 08:28:37 --> Hooks Class Initialized
DEBUG - 2018-05-18 08:28:37 --> UTF-8 Support Enabled
INFO - 2018-05-18 08:28:37 --> Utf8 Class Initialized
INFO - 2018-05-18 08:28:37 --> URI Class Initialized
INFO - 2018-05-18 08:28:37 --> Router Class Initialized
INFO - 2018-05-18 08:28:37 --> Output Class Initialized
INFO - 2018-05-18 08:28:37 --> Security Class Initialized
DEBUG - 2018-05-18 08:28:37 --> Global POST, GET and COOKIE data sanitized
INFO - 2018-05-18 08:28:37 --> Input Class Initialized
INFO - 2018-05-18 08:28:37 --> Language Class Initialized
INFO - 2018-05-18 08:28:37 --> Loader Class Initialized
INFO - 2018-05-18 08:28:37 --> Helper loaded: url_helper
INFO - 2018-05-18 08:28:37 --> Helper loaded: file_helper
INFO - 2018-05-18 08:28:37 --> Helper loaded: form_helper
INFO - 2018-05-18 08:28:37 --> Database Driver Class Initialized
INFO - 2018-05-18 08:28:37 --> Email Class Initialized
DEBUG - 2018-05-18 08:28:37 --> Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.
INFO - 2018-05-18 08:28:37 --> Session: Class initialized using 'files' driver.
INFO - 2018-05-18 08:28:37 --> Form Validation Class Initialized
INFO - 2018-05-18 08:28:37 --> Controller Class Initialized
INFO - 2018-05-18 08:28:37 --> Model "User_model" initialized
INFO - 2018-05-18 08:28:37 --> Model "Cookie_model" initialized
INFO - 2018-05-18 08:28:37 --> Helper loaded: string_helper
INFO - 2018-05-18 08:28:37 --> Helper loaded: cookie_helper
INFO - 2018-05-18 08:28:37 --> Language file loaded: language/english/form_validation_lang.php
INFO - 2018-05-18 08:28:37 --> Final output sent to browser
DEBUG - 2018-05-18 08:28:37 --> Total execution time: 0.1759

如您所见,在日志文件中根本没有错误消息

我的Google Chrome设置

enter image description here

1 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

尝试使用数组方法(替代方案),将关联数组传递给set_cookie

替换

$cookie = array(
    'name'      => 'pre',
    'value'     => $token,
    'expire'    => 86400 * 7,
);
$this->input->set_cookie(
    $cookie['name'], $cookie['value'], $cookie['expire']
);

有了这个:

$cookie = array(
    'name'      => 'pre',
    'value'     => $token,
    'expire'    => 86400 * 7,
    /* no need to set domain if localhost */
    'domain' => '.localhost',
    'path'   => '/',
    'prefix' => 'e_',
    'secure' => FALSE
);
$this->input->set_cookie($cookie);

查看您的Cookie

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

参考:https://www.codeigniter.com/user_guide/libraries/input.html