检查CodeIgniter中是否存在Cookie [英] Check if cookie exists in CodeIgniter

查看:122
本文介绍了检查CodeIgniter中是否存在Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的搜索系统,在PHP中,将搜索的文本添加到数组,并将其放在cookie使用json_encode()。



是:我需要检查cookie是否已经存在,如果没有,创建它。



我使用以下代码来验证是否存在,但没有成功:

  {
$ search = $ this-> input-> post('search_text');
$ types = $ this-> input-> post('search_type');
$ checkboxes =;
if(!empty($ types))
{
foreach($ types as $ v)
$ checkboxes = $ v。
}

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

// cookie的数组
$ search_history = array();
array_push($ search_history,$ search);

// cookie check 1
if(get_cookie('search')!=''){
echocookie exists;
} else
echocookie does not exist;

//设置cookie
$ cookie = array(
'name'=>'search',
'value'=> json_encode($ search_history) ,
'expire'=> time()+ 86500
);
set_cookie($ cookie);

// cookie check 2
if(get_cookie('search')!=''){
echocookie exists;
} else
echocookie does not exist;

// echo get_cookie('search');

//redirect('search/'.urlencode($search).'/'.urlencode($checkboxes));
}



我可以创建cookie并获取它的值,



已尝试过:

  if(get_cookie('search')!='')

  if(get_cookie('search')!= null)
pre>

并使用

  if(get_cookie('search') )

但两者都不起作用。






编辑:



正如建议,我现在使用这个,它不创建cookie。 / p>

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

// cookie的数组
$ search_history = array();
array_push($ search_history,$ search);

if(cookie('search')== false){
//设置cookie
$ cookie = array(
'name'=>'search ',
'value'=> json_encode($ search_history),
'expire'=> time()+ 86500
);
set_cookie($ cookie);
}






问题解决。

  //检查Cookie是否存在
if($ this-> input-> cookie('cookiename')!=''){
// exists
}


解决方案

使用get_cookie()

  if(is_null(get_cookie('cookiename'))){
//设置您的cookie在这里
}


I'm using a search system, in PHP, that adds the text searched to an array and put it inside a cookie using json_encode().

The problem is: I need to check if the cookie already exists and, if not, create it.

I'm using the following code to simply verify if it exists but without success:

{
        $search     = $this->input->post('search_text');
        $types      = $this->input->post('search_type');
        $checkboxes = "";
        if(!empty($types))
        {
            foreach($types as $v)
                $checkboxes = $v.",";
        }

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

        //cookie's array
        $search_history = array();
        array_push($search_history, $search);

        //cookie check 1
        if(get_cookie('search')!=''){
            echo "cookie exists";
        }else
            echo "cookie doesn't exist";

        // set cookie 
        $cookie = array(
            'name'   => 'search',
            'value'  => json_encode($search_history),
            'expire' => time()+86500
        );
        set_cookie($cookie);

        //cookie check 2
        if(get_cookie('search')!=''){
            echo "cookie exists";
        }else
            echo "cookie doesn't exist";

        //echo get_cookie('search');

        //redirect('search/'.urlencode($search).'/'.urlencode($checkboxes)); 
    }

I can create the cookie and get it's value, but I can't seem to find a way to check if it's already created in PHP code.

Have already tried with:

if(get_cookie('search')!='')

,

if(get_cookie('search')!=null)

and with

if(get_cookie('search'))

but neither of those seem to work.


EDIT:

As suggested, I'm now using this and it doesn't create the cookie.

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

        //cookie's array
        $search_history = array();
        array_push($search_history, $search);

        if(cookie('search') == false){
            // set cookie 
            $cookie = array(
                'name'   => 'search',
                'value'  => json_encode($search_history),
                'expire' => time()+86500
            );
            set_cookie($cookie);
        }            


Final EDIT

Problem solved.

 //checks if the cookie exists
        if($this->input->cookie('cookiename')!=''){
            //exists
        }

解决方案

use get_cookie()

if (is_null(get_cookie('cookiename'))) {
 //set yours cookie here
}

这篇关于检查CodeIgniter中是否存在Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆