dongxu0690 2018-05-17 22:17
浏览 97
已采纳

CI 3.1.8 set_cookie()不起作用?

I know there are so many question about this set_cookie() CI, but I can't found the answer, the cookie doesn't showing in Google Cookies Data menu.

I know how to read it, but it doesn't showing at all in the browser cookies data.

in 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']
);

The setting in config.php

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

This website Project is under project.com folder, this is what it looks like

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

But there are no cookie data for [::1] nor .localhost, why does this happening?

I already tried to googled it but can't found the solution, I already tried with

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

But it doesn't work either.

Adding 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

As you can see, in the log file there are no error message at all

My Google chrome setting

enter image description here

展开全部

  • 写回答

1条回答 默认 最新

  • douweinu8562 2018-05-17 22:52
    关注

    Hope this will help you :

    Try Using Array Method (Alternative), an associative array is passed to set_cookie:

    Replace it

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

    With this :

    $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);
    

    To check your cookies

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

    Reference : https://www.codeigniter.com/user_guide/libraries/input.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部