Wordpress 在一页之间丢失 cookie


Wordpress loses cookies from page to page

我为wordpress创建了一个插件。下面是一个示例代码。该代码运行良好,并成功地将数据存储在 cookie 中。但问题是当我刷新页面时,旧数据丢失了,它再次写入相同的数据......

下面的代码块位于插件文件中。

if (!isset($_COOKIE['selection_list'])) {
        setcookie('selection_list', '', time() + 3600, "/dev/");
    }

下面的代码块在 AJAX 调用 URL 文件中,数据使用 AJAX 发送到文件

if (!isset($_COOKIE['selection_list'])) {
$_COOKIE['selection_list'] = array();
 }
array_push($_COOKIE['selection_list'], "some_test_data");

在此之后,数组长度或数组元素的值被发回。每次刷新页面时,它都应该推送数组中的数据,并将数组大小增加一个。但实际上它没有... :(。它覆盖上面的同一行,并且数组长度始终保持 1,不会递增。:(请让我知道我在代码中做错了什么吗?我的网站网址 http://flintimm.cluster013.ovh.net/dev/

更新::

这是插件文件中的代码

<?php
/*
Plugin Name: Selections List
Plugin URI:
Description: Displays a list of your selected properties
Version: 1.23
Author: Muhammad Sohail
Author URI: https://www.elance.com/s/sohailx2x/10183/
*/
function selection_list_start($post_id) {
if (is_single()) { // when a single post page is opened
    ?>
    <script src='<?php echo plugins_url(); ?>/selections-list/script.js'>    </script>
    <link rel='stylesheet' type='text/css' href='<?php echo plugins_url(); ?>/selections-list/style.css' />
    <?php
    $property_ID = get_the_ID();
    $content = get_the_content();
    if (!isset($_COOKIE['selection_list'])) {
        setcookie('selection_list', '', time() + 3600, "/");
    }
    $post = get_post($property_ID);
    $meta_field = get_post_meta($property_ID);
        $post_title = $post->post_title;
        $post_link = $post->guid;
        $property_price = strtolower($meta_field['REAL_EXPERT_property_price'][0]);
    $found = 0;
    for($index = 0; $index < count($_SESSION['selection_list']); $index++) {
        if ($property_ID == $_SESSION['selection_list'][$index]) {
            $found = 1;
        }
    }
    if ($found) {
        $content .= "<br /><span id='"add-to-my-selection'" class='"meta-print visible-desktop'">Ajouté à la sélection</span>";
    } else {
        $content .= "<br /><span id='"add-to-my-selection'" class='"meta-print visible-desktop'">Ajouter à ma sélection</span>";
    }
    $content = $content . "<input type='hidden' id='property-ID' value = '$property_ID' />";
    $content = $content . "<input type='hidden' id='post-title' value = '$post_title' />";
    $content = $content . "<input type='hidden' id='post-link' value = '$post_link' />";
    $content = $content . "<input type='hidden' id='property-price' value = '$property_price' />";
    $content = $content . "<input type='hidden' id='cookieee' value = '" . $_COOKIE['my_cookie'][0] . "' />";
    return $content;} add_action('the_content', 'selection_list_start'); ?>

这是 AJAX URL 路径文件中的代码...

    <?php
session_start();
$str = "";
$property_id = $_POST['property_id'];
if (!isset($_COOKIE['selection_list'])) {
    $_COOKIE['selection_list'] = array();
}
array_push($_COOKIE['selection_list'], $_POST['property_id']); // this doesn't increment the array size with page refresh...
array_push($_SESSION['selection_list'], $_POST['property_id']); // this increments the array size with page refresh...
if (isset($_POST['session']) && $_POST['session'] == "start" && $_POST['task'] == "add") {
    if (isset($_SESSION['selection_list'])) {
        array_push($_SESSION['selection_list'], $property_id);
    } else {
        $_SESSION['selection_list'] = array();
        array_push($_SESSION['selection_list'], $property_id);
    }
    $str = "";
    for ($counter = 0; $counter < count($_SESSION['selection_list']); $counter++) {
        $str .= $_SESSION['selection_list'][$counter] . "<br />";
    }
    //echo $str;
    echo count($_SESSION['selection_list']);
}
if (isset($_POST['session']) && $_POST['session'] == "get" && count($_SESSION['selection_list']) > 0) {
    if (isset($_SESSION['selection_list'])) {
    echo count($_SESSION['selection_list']) . " | " . count($_COOKIE['selection_list']);
    // when I refresh page, the above line prints following output with each page refresh
    /*
    1 | 1
    2 | 1
    3 | 1
    4 | 1
    ...
    and so on...
    */
    }
} else {
    echo "Not set...";
}
if (isset($_POST['session']) && $_POST['session'] == "end") { // if session start is not passed, then session end will be passed
    if (isset($_SESSION['selection_list'])) {
        session_destroy();
        echo "Session destroyed";
    } else {
        echo "No session";
    }
}
?>

ajax 运行良好,正确传递数据,并正确显示数据。唯一的问题是饼干。

尝试通过插件中的$_COOKIE['selection_list'] = array();来更改setcookie('selection_list', '', time() + 3600, "/dev/");