如果值不存在,则在 php 中附加 Cookie
if value is not present Append Cookies in php
Append value Cookies if value is not present
I am trying to insert cookie logic is simple
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
$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));
}
> 已编辑 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));
}
}
首先,您将 $current_value 设置为字符串 '',然后使用数组语法添加新元素。您应该在开始时将 $current_value 设置为 array()。
另外,您在第二个 setcookie() 调用“$current_valu”中有错字,应该是“$current_value”。
这里是代码的一些改进版本(在我看来)。不过还没有测试过。
$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));
}
编辑:因为原来的 post 更新了一个函数定义,这里有一个关于它的评论。您定义了一个函数 Details() 但它没有 return 一个值。函数应该做什么?也许应该 return $current_value;
或 return implode(',', $current_value);
?另外,$uid 参数似乎未被使用,因此可以将其删除。
Append value Cookies if value is not present I am trying to insert cookie logic is simple
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
$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));
}
> 已编辑 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));
}
}
首先,您将 $current_value 设置为字符串 '',然后使用数组语法添加新元素。您应该在开始时将 $current_value 设置为 array()。
另外,您在第二个 setcookie() 调用“$current_valu”中有错字,应该是“$current_value”。
这里是代码的一些改进版本(在我看来)。不过还没有测试过。
$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));
}
编辑:因为原来的 post 更新了一个函数定义,这里有一个关于它的评论。您定义了一个函数 Details() 但它没有 return 一个值。函数应该做什么?也许应该 return $current_value;
或 return implode(',', $current_value);
?另外,$uid 参数似乎未被使用,因此可以将其删除。