【问题标题】:Wordpress loses cookies from page to pageWordpress 在页面之间丢失 cookie
【发布时间】:2014-01-06 04:13:00
【问题描述】:

我为 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。

【问题讨论】:

  • 试图接收响应的ajax文件在同一个目录下?例如:/dev/(anything-ajax.php) 文件?
  • 最近看到Ajax调用不与主站点共享Cookie的问题,我发现原因是Ajax是从Javascript调用的,其路径与普通浏览器的路径不同。坦率地说,我不记得我是否在我的网站中解决了这个问题,但你可以朝那个方向搜索。
  • 刚刚为您找到了一些东西:stackoverflow.com/questions/8854816/…
  • @Shazzad 这是我的 FileZilla 中所有文件(ajax 文件、php 文件、jquery 文件和 css 文件)所在的 URL。 /www/dev/wp-content/plugins/selections-list 文件是 ajax-data.php script.js selections.php style.css

标签: php jquery ajax wordpress cookies


【解决方案1】:

尝试在您的插件中将setcookie('selection_list', '', time() + 3600, "/dev/"); 更改为$_COOKIE['selection_list'] = array();

【讨论】:

  • 我需要更多的全球背景,你能分享更多吗?
  • 感谢您有兴趣帮助我。我已经更新了上面的帖子。
猜你喜欢
  • 2013-12-10
  • 2019-04-04
  • 2016-07-02
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2019-07-11
相关资源
最近更新 更多