1

I have created a plugin for wordpress. Below is a sample code. The code works well and stores data in cookies successfully. But the problem is when I refresh the page, the old data is lost and it write the same data again...

The below block of code is in the plugin file.

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

The below block of code is in the AJAX call URL file, the data is sent to the file using AJAX

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

array_push($_COOKIE['selection_list'], "some_test_data");

and after this, the array length or the values of array element is sent back. Every time I refresh the page, It should push the data in array, and increasing the array size by one. But actually it doesn't... :(. It overwrite the same line above, and the array length always remain 1, doesn't increment. :( Please let me know is there something I am doing wrong in the code? My website URL is http://flintimm.cluster013.ovh.net/dev/

Updated::

Here is the code in plugin file

<?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'); ?>

and here is the code in AJAX URL path file...

    <?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";
    }
}

?>

The ajax is wrking well, passes data correctly, and displays data correctly. The only problem is with cookies.

Muhammad Sohail
  • 191
  • 2
  • 2
  • 6
  • the ajax file that trying to receive the response is on the same directory ? ex: /dev/(anything-ajax.php) file ? – Shazzad Dec 18 '13 at 06:40
  • Recently I have see a problem that Ajax call do not share the Cookie with main site, the reason I found is Ajax is been called from Javascript and their Path is different from that of normal Browser. Frankly I didn't recall if I solve this issue in my site, but you can search in that direction. – Sumit Gupta Dec 18 '13 at 07:23
  • just found something for you: http://stackoverflow.com/questions/8854816/keeping-the-cookie-after-a-cross-domain-ajax-request – Sumit Gupta Dec 18 '13 at 07:25
  • @Shazzad Here is the URL in my FileZilla where all files (ajax file, php file, jquery file and css file) are located. /www/dev/wp-content/plugins/selections-list The files are ajax-data.php script.js selections.php style.css – Muhammad Sohail Dec 18 '13 at 07:44

1 Answers1

0

Try change setcookie('selection_list', '', time() + 3600, "/dev/"); by $_COOKIE['selection_list'] = array(); in your plugin.

Ignacio Ocampo
  • 2,693
  • 1
  • 20
  • 31