cakephp cookie无法正常工作或未在服务器上设置但在localhost上工作

时间:2014-11-14 06:36:15

标签: php cakephp cookies

在我的项目中, 我想添加收藏夹或删除所选属性的收藏夹

下面的代码我曾经做过任务..它在localhost上工作得很好..

public function add_favourites(){

   if($this->request->is('ajax') or $this->request->is('post')){  
   $Fav_cookie = $this->Cookie->read('Fav_cookie');
    if(isset($Fav_cookie)){
         $add_fav = $this->request->data['prop_id'];
         $Fav_cookie .= "_".$add_fav;    
         $this->Cookie->write('Fav_cookie', $Fav_cookie, false, '1 Month');
     }else{
         $add_fav = $this->request->data['prop_id'];
         $this->Cookie->write('Fav_cookie', $add_fav, false, '1 Month'); 
     }
     $count_fav = $this->Cookie->read('Fav_cookie');
     $count_fav1 = explode("_",$count_fav);
     echo count($count_fav1);
   }
   exit;

}

public function remove_favourites(){

   if($this->request->is('ajax') or $this->request->is('post')){

     $count_fav = $this->Cookie->read('Fav_cookie');
     $count_fav1 = explode("_",$count_fav);
     if(isset($count_fav1)){

          $remove_fav = $this->request->data['prop_id'];
          if(($key = array_search($remove_fav,$count_fav1)) !== false) {
              unset($count_fav1[$key]);

               }
           }
           echo count($count_fav1);
           $count_fav2= implode("_",$count_fav1);
           $this->Cookie->write('Fav_cookie', $count_fav2, false, '1 Month');
        }
        exit;
   }

但我在实时服务器上使用相同的代码...

添加收藏夹效果很好..但删除收藏夹不能正常工作..

问题是仅限实时服务器

删除收藏夹后,

cookie未设置或写入。

抱歉我的英语......

请帮我解决一下......

1 个答案:

答案 0 :(得分:0)

我在if条件这两行之间改变了我的代码

 $count_fav2= implode("_", $count_fav1);
 $this->Cookie->write('Fav_cookie', $count_fav2, false, '1 Month');

代码:

public function remove_favourites(){

  if($this->request->is('ajax') or $this->request->is('post')){

       $count_fav = $this->Cookie->read('Fav_cookie');
       $count_fav1 = explode("_",$count_fav);
       if(isset($count_fav1)){
           $remove_fav = $this->request->data['prop_id'];
           if(($key = array_search($remove_fav,$count_fav1)) !== false) {
                unset($count_fav1[$key]);
           }
           $count_fav2= implode("_",$count_fav1);
           $this->Cookie->write('Fav_cookie', $count_fav2, false, '1 Month');
       }
       echo count($count_fav1);
    }
    exit;


  }

现在它在本地和实时服务器上工作..

感谢