当我按下按钮时,如何才能从Cookie数组中获取值?

时间:2017-04-26 作者:W. White

我制作了一个最喜欢的插件。通过一个快捷码,我可以在任何我想要的地方显示“愿望/收藏夹列表”的按钮。此按钮用于在cookie数组中添加页面/帖子/博客/文章等的post\\u id,代码如下:

<?php 
 if (isset($_POST[\'submit_wishlist\'])){
  if (!isset($_COOKIE[\'favorites\'])){
   //echo \'not set <br>\';
   $cookie_value = get_the_ID();
   $init_value = array($cookie_value);
   $init_value = serialize($init_value);
   //echo $init_value;
   setcookie(\'favorites\', $init_value, time() + (86400 * 30), "/");
   wp_redirect($_SERVER[\'HTTP_REFERER\']);
 } else {
   //echo \'set <br>\';
   $cookie_value = get_the_ID();
   $prev_value = $_COOKIE[\'favorites\'];
   $prev_value = stripslashes($prev_value);
   $prev_value = unserialize($prev_value);

   array_push($prev_value, $cookie_value);
   $new_value = serialize($prev_value);
   //echo $new_value;
   setcookie(\'favorites\', $new_value, time() + (86400 * 30), "/");
   wp_redirect($_SERVER[\'HTTP_REFERER\']);
 }
} 
?>
这工作正常,post\\u id存储在cookie数组中。使用代码print_r(unserialize($_COOKIE[\'favorites\'])); 我能够打印Cookie并获得所有存储的post\\u id的概览。

Problem/Question
目前,我添加了一个新的快捷码,用于显示收藏夹列表。该列表的每个值都将获得一个用于删除/取消设置该cookie的垃圾按钮。现在,我需要使用以下代码:

<?php 
$all_favorites= unserialize($_COOKIE[\'favorites\']);

echo \'<table>\';
foreach($all_favorites as $key => $value) {
 echo \'<tr>\';
  echo \'Post-ID = \' . $value . \' \';
  ?>
  <form method="POST"><button type="submit" class="btn btn-default" name="delete"><span class="glyphicon glyphicon-trash"></span></button>
  <input type="hidden" name="delete_id" value="<?php echo $value; ?>" />
  </form><br>
  <?php
 echo \'</tr>\';
}
echo \'</table>\';
if (isset($_POST[\'delete\'])){
  //function for setting new cookie, function is displayed on each page before the get_header()
  set_cookie_delete();
}
?>
这部分代码的输出:

enter image description here

Edit
功能:

<?php
function set_cookie_delete(){
  $all_favorites = unserialize($_COOKIE[\'favorites\']);
  $delete_id = $_POST[\'delete_id\'];
  echo \'deleted value = \' . \' \' . $delete_id . \'<br>\';

  $array_delete = array_diff($all_favorites, array($delete_id));
  $array_delete = serialize($array_delete);
  print_r($array_delete);

  wp_redirect($_SERVER[\'HTTP_REFERER\']);
  setcookie(\'favorites\', $array_delete, time() + (86400 * 30), "/");

  //echo \'<br><br>\';
  //print_r($_COOKIE);

  }
  ?>
我不明白的是setcookie(); 不工作。它位于页面的开头,我首先刷新页面,以便可以设置cookie,对吗?

我们将感谢您的帮助,提前感谢!

2 个回复
SO网友:Laxmana

您需要将每个项目的id放入表单中,以指示将删除的项目。

<?php 
$all_favorites= unserialize($_COOKIE[\'favorites\']);

echo \'<table>\';
foreach($all_favorites as $key => $value) {
 echo \'<tr>\';
  echo \'Post-ID = \' . $value . \' \';
  ?>
  <form method="POST">
    <input type="hidden" name="id" value="<?php echo $value; ?>">
    <button type="submit" class="btn btn-default" name="delete">
        <span class="glyphicon glyphicon-trash"></span>
    </button>
  </form><br>
  <?php
 echo \'</tr>\';
}
echo \'</table>\';
if (isset($_POST[\'delete\'])){
   $id = $_POST[\'id\']; // do security checks (sanitize etc)
   // unset post with $id from cookie

}
?>

SO网友:W. White

SOLUTION

首先,非常感谢拉克斯马纳把我推向了正确的方向!

我有这个功能set_cookie_delete() 对于此代码:
函数

<?php
  if (isset($_POST[\'delete\'])){
  $all_favorites = unserialize($_COOKIE[\'favorites\']);
  $delete_id = $_POST[\'delete_id\'];
  //echo \'deleted value = \' . \' \' . $delete_id . \'<br>\';

  $array_delete = array_diff($all_favorites, array($delete_id));
  $array_delete = serialize($array_delete);
  //print_r($array_delete);

  setcookie(\'favorites\', $array_delete, time() + (86400 * 30), "/");
  wp_redirect($_SERVER[\'HTTP_REFERER\']);
 }
?>

相关推荐

Setting Cookies

我创建了两个主题(浅色和深色),但我不知道如何设置cookie,以便用户在页面之间导航或下周返回站点时,主题保持不变。是否有一个简单的脚本可以添加到现有代码中?HTML<html> <head> <link id=\"lightTheme\" rel=\"stylesheet\" type=\"text/css\" href=\"styleSheetLightTheme.css\"> <link id=\"darkTheme\" r