doulei8861 2013-04-30 16:52
浏览 28
已采纳

codeigniter:如何使用其名称检索cookie值

I am trying to get the cookie value by its name. This is the first time i am using the cookie in codeigniter .

Here is how i am trying to get the cookie value which i have set, but its show output in my view page as bool(false)

How i am setting cookie into my controller....

 $ranum=random_string('alnum', 20);//generates random numbers/strings  
   $cookie = array(
        'name'   => 'remember_me_token',
        'value'  =>$ranum,
        'expire' => '1209600',  // Two weeks
        'domain' => base_url(),  //my base url is http://localhost
        'path'   => '/'
    ); 
    set_cookie($cookie);

How i am trying trying to get my cookie value is

<?php echo $this->input->cookie('remember_me_token', TRUE);  //giving output like bool(false)  ?>

Note: i am using ajax when users click the "submit" button

Please help me how to check if cookie is available with this name, if its available then how to retrieve its value into my view page..

Update: I checked my browser cookies,its looking like this...its seems i have problem in setting cookie. If so then please help me why my cookie is not setting?

enter image description here

  • 写回答

1条回答 默认 最新

  • doutan2456 2013-04-30 17:07
    关注

    The Cookie is not available on the same request/page where you set it. Once you set a cookie it must be sent to browse.

    From PHP.NET

    Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter.

    Update:

    Base url should be something like yourdomain.com. Check the $config['base_url'] in your config.php file. You can't use $config['base_url']=http://localhost/, you should set it as http://localhost/yourdomain/ or keep it blank.

    |--------------------------------------------------------------------------
    | Base Site URL
    |--------------------------------------------------------------------------
    |
    | URL to your CodeIgniter root. Typically this will be your base URL,
    | WITH a trailing slash:
    |
    |   http://example.com/
    |
    | If this is not set then CodeIgniter will guess the protocol, domain and
    | path to your installation.
    |
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?