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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100