如果值不存在则在php中添加Cookie

时间:2021-08-13 21:20:34

Append value Cookies if value is not present I am trying to insert cookie logic is simple

如果值不存在,则附加值Cookie我试图插入cookie逻辑很简单

check if the value is present . if not insert new value along with old value in comma seperated form . i have tried some code but,unable to get correct result -in this code a new value should be inserted,which is hapening but not getting old value

检查值是否存在。如果没有以逗号分隔形式插入新值和旧值。我已经尝试了一些代码但是,无法得到正确的结果 - 在这段代码中应该插入一个新值,这是hapening但没有得到旧值

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

> edited 2

>编辑2

 public function Details($id,$uid){
        $cookie_name = 'blog_id_cookie';

if (isset($_COOKIE[$cookie_name])) {
  $current_value = explode(',', $_COOKIE[$cookie_name]);
} else {
  $current_value = array();
}

if (!in_array($id, $current_value)) {
  $current_value[] = $id;
  setcookie($cookie_name, implode(',', $current_value));
}
}

1 个解决方案

#1


First you are setting $current_value to a string '' but then using array syntax to add a new element. You should be setting $current_value to array() in the beginning instead.

首先,您将$ current_value设置为字符串'',然后使用数组语法添加新元素。您应该在开头将$ current_value设置为array()。

Also you have a typo at the second setcookie() call "$current_valu" which should be "$current_value".

你也有第二个setcookie()调用“$ current_valu”的拼写错误,它应该是“$ current_value”。

Here is a bit improved version of the code (in my opinion). Haven't tested this though.

这是代码的一些改进版本(在我看来)。虽然没有测试过这个。

$cookie_name = 'blog_id_cookie';

if (isset($_COOKIE[$cookie_name])) {
  $current_value = explode(',', $_COOKIE[$cookie_name]);
} else {
  $current_value = array();
}

if (!in_array($id, $current_value)) {
  $current_value[] = $id;
  setcookie($cookie_name, implode(',', $current_value));
}

Edit: As the original post was updated with a function definition here is a comment about that. You define a function Details() but it does not return a value. What should the function do? Maybe it should return $current_value; or return implode(',', $current_value);? Also the $uid parameter seems to be unused so it could be removed.

编辑:由于原始帖子是使用函数定义更新的,因此这里有一个评论。您定义函数Details()但它不返回值。该功能应该做什么?也许它应该返回$ current_value;或者返回implode(',',$ current_value);?此外,$ uid参数似乎未使用,因此可以将其删除。

#1


First you are setting $current_value to a string '' but then using array syntax to add a new element. You should be setting $current_value to array() in the beginning instead.

首先,您将$ current_value设置为字符串'',然后使用数组语法添加新元素。您应该在开头将$ current_value设置为array()。

Also you have a typo at the second setcookie() call "$current_valu" which should be "$current_value".

你也有第二个setcookie()调用“$ current_valu”的拼写错误,它应该是“$ current_value”。

Here is a bit improved version of the code (in my opinion). Haven't tested this though.

这是代码的一些改进版本(在我看来)。虽然没有测试过这个。

$cookie_name = 'blog_id_cookie';

if (isset($_COOKIE[$cookie_name])) {
  $current_value = explode(',', $_COOKIE[$cookie_name]);
} else {
  $current_value = array();
}

if (!in_array($id, $current_value)) {
  $current_value[] = $id;
  setcookie($cookie_name, implode(',', $current_value));
}

Edit: As the original post was updated with a function definition here is a comment about that. You define a function Details() but it does not return a value. What should the function do? Maybe it should return $current_value; or return implode(',', $current_value);? Also the $uid parameter seems to be unused so it could be removed.

编辑:由于原始帖子是使用函数定义更新的,因此这里有一个评论。您定义函数Details()但它不返回值。该功能应该做什么?也许它应该返回$ current_value;或者返回implode(',',$ current_value);?此外,$ uid参数似乎未使用,因此可以将其删除。