【问题标题】:CI 3.1.8 set_cookie() doesn't work?CI 3.1.8 set_cookie() 不起作用?
【发布时间】:2018-05-18 15:11:54
【问题描述】:

我知道有很多关于这个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

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

我的谷歌浏览器设置

【问题讨论】:

    标签: php codeigniter cookies


    【解决方案1】:

    希望对您有所帮助:

    尝试使用数组方法(Alternative),将关联数组传递给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

    【讨论】:

    • 啊,我明白了,为什么它不设置它是因为我使用域并输入路径值,但如果我只是将 domain 留空并放入 / path 会好的!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    相关资源
    最近更新 更多