dryb38654 2015-05-04 08:10
浏览 136

如果值不存在,则插入cookie

I am trying to insert cookie. The logic is simple as:

  • Check if the value is present .
  • If not, insert new value with comma separated form.

I have tried some code but I could not able to get a correct result.

In this code a new value should be inserted, which is happening but not getting old value.

Edited Code 2

        $current_value = '';
 if(!isset($_COOKIE['blog_id_cookie'])){
     setcookie('blog_id_cookie', $id);
     $current_value[] = $_COOKIE['blog_id_cookie'];
 } else {
     $current_value = explode(',', $_COOKIE['blog_id_cookie']);
 } 
 if(!in_array($id, $current_value)){
     $current_value[] = $id;
     $cookie_name  = "blog_id_cookie";
     setcookie($cookie_name, implode(',', $current_valu));
 }
  • 写回答

1条回答 默认 最新

  • duanhong1985 2015-05-04 08:17
    关注

    With that code $current_value will be defined only if the cookies is not set. Set $current_value in the else part. Concatenate the cookie instead of $current_value. Try with -

    Update

     $current_value = '';
     if(!isset($_COOKIE['blog_id_cookie'])){
         setcookie('blog_id_cookie', $id);
         $current_value[] = $_COOKIE['blog_id_cookie'];
     } else {
         $current_value = explode(',', $_COOKIE['blog_id_cookie']);
     } 
     if(!in_array($id, $current_value)){
         $current_value[] = $id;
         $cookie_name  = "blog_id_cookie";
         setcookie($cookie_name, implode(',', $current_value));
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统