簡體   English   中英

如何在codeigniter中設置cookie

[英]how to set cookie in codeigniter

我嘗試按照以下代碼設置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);                   
   }

以下代碼用於獲取Cookie

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

誰能告訴我這是什么問題? 提前致謝。

采用

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

而不是set_cookie($ cookie);

您需要創建一個控制器類,並向其中添加以下代碼;

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

   }

}

上面的代碼通過以下方式設置Cookie

$this->input->set_cookie()

使用以下命令加載幫助程序:

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

您可以在以下位置閱讀更多內容: 在Codeigniter中設置Cookie

    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');
    }

在視圖頁面中調用,例如echo $this->input->cookie('user');

輸出= Pradip

說數據

$my_cookie= array(

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

   );

采用

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

代替

set_cookie($my_cookie);

首先將此行添加到控制器頂部

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

然后將cookie設置為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM