php 如何在codeigniter中设置cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34132448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:46:35  来源:igfitidea点击:

how to set cookie in codeigniter

phpcodeigniter

提问by kc1994

i tried following code for set cookie but i can't get the cookie.

我尝试使用以下代码设置 cookie,但无法获取 cookie。

if($this->input->post('remember')){                    
                $this->load->helper('cookie');
                $cookie = array(
                        'name'   => 'remember_me',
                        'value'  => 'test',                            
                        'expire' => '300',                                                                                   
                        'secure' => TRUE
                        );
               set_cookie($cookie);                   
   }

and following code is for get cookie

以下代码用于获取cookie

$cookie= get_cookie('remember_me');  
 var_dump($cookie);

can any one tell me what's the problem is there? thanks in advance.

任何人都可以告诉我有什么问题吗?提前致谢。

回答by momouu

Use

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

instead of set_cookie($cookie);

而不是 set_cookie($cookie);

回答by Shahroze Nawaz

You need to create a controller class and add the following code to it;

您需要创建一个控制器类并在其中添加以下代码;

<?php

if ( ! defined('BASEPATH')) exit('Stop Its demostrate how to set cookie');

class cw_cookies extends CI_Controller {

???function __construct()

???{

???????parent::__construct();

???????$this->load->helper('cookie');

???}



???function set()

???{

???????$cookie= array(

???????????'name'   => 'remember_me',
           'value'  => 'test',                            
           'expire' => '300',                                                                                   
           'secure' => TRUE

???????);

???????$this->input->set_cookie($cookie);

???????echo "Congratulation Cookie Set";

???}



???function get()

???{

???????echo $this->input->cookie('remember_me',true);

???}

}

The above code sets the cookies through

上面的代码通过设置cookies

$this->input->set_cookie()

The helper is loaded using:

使用以下方法加载帮助程序:

$this->load->helper('cookie');

You can read more at : Set Cookies in Codeigniter

您可以在以下位置阅读更多信息:在 Codeigniter 中设置 Cookie

回答by Pradipsinh Rajput

    public function cookie()
    {
        $this->load->helper('cookie');

        $name   = 'user';
        $value  = 'pradip';
        $expire = time()+1000;
        $path  = '/';
        $secure = TRUE;

        setcookie($name,$value,$expire,$path); 

        $this->load->view('welcome_message');
    }

Call in view page like echo $this->input->cookie('user');

调用视图页面,如 echo $this->input->cookie('user');

output = pradip

输出 = pradip

回答by Sani Kamal

Say data

说数据

$my_cookie= array(

       'name'   => 'remember_me',
       'value'  => 'test value',                            
       'expire' => '3000',                                                                                   
       'secure' => TRUE

   );

Use

$this->input->set_cookie($my_cookie);

instead of

代替

set_cookie($my_cookie);

回答by LIsa

first add this line to the top of your controller

首先将此行添加到控制器的顶部

function __construct(){
    parent::__construct();
    $this->load->helper(array('cookie', 'url'));
}

and then set cookie as

然后将cookie设置为

set_cookie('counters','ok','999600');