Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress loses cookies from page to page
    primarykey
    data
    text
    <p>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...</p> <p>The below block of code is in the plugin file.</p> <pre><code>if (!isset($_COOKIE['selection_list'])) { setcookie('selection_list', '', time() + 3600, "/dev/"); } </code></pre> <p>The below block of code is in the AJAX call URL file, the data is sent to the file using AJAX</p> <pre><code>if (!isset($_COOKIE['selection_list'])) { $_COOKIE['selection_list'] = array(); } array_push($_COOKIE['selection_list'], "some_test_data"); </code></pre> <p>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 <a href="http://flintimm.cluster013.ovh.net/dev/" rel="nofollow">http://flintimm.cluster013.ovh.net/dev/</a></p> <p>Updated::</p> <p>Here is the code in plugin file</p> <pre><code>&lt;?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 ?&gt; &lt;script src='&lt;?php echo plugins_url(); ?&gt;/selections-list/script.js'&gt; &lt;/script&gt; &lt;link rel='stylesheet' type='text/css' href='&lt;?php echo plugins_url(); ?&gt;/selections-list/style.css' /&gt; &lt;?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-&gt;post_title; $post_link = $post-&gt;guid; $property_price = strtolower($meta_field['REAL_EXPERT_property_price'][0]); $found = 0; for($index = 0; $index &lt; count($_SESSION['selection_list']); $index++) { if ($property_ID == $_SESSION['selection_list'][$index]) { $found = 1; } } if ($found) { $content .= "&lt;br /&gt;&lt;span id=\"add-to-my-selection\" class=\"meta-print visible-desktop\"&gt;Ajouté à la sélection&lt;/span&gt;"; } else { $content .= "&lt;br /&gt;&lt;span id=\"add-to-my-selection\" class=\"meta-print visible-desktop\"&gt;Ajouter à ma sélection&lt;/span&gt;"; } $content = $content . "&lt;input type='hidden' id='property-ID' value = '$property_ID' /&gt;"; $content = $content . "&lt;input type='hidden' id='post-title' value = '$post_title' /&gt;"; $content = $content . "&lt;input type='hidden' id='post-link' value = '$post_link' /&gt;"; $content = $content . "&lt;input type='hidden' id='property-price' value = '$property_price' /&gt;"; $content = $content . "&lt;input type='hidden' id='cookieee' value = '" . $_COOKIE['my_cookie'][0] . "' /&gt;"; return $content;} add_action('the_content', 'selection_list_start'); ?&gt; </code></pre> <p>and here is the code in AJAX URL path file...</p> <pre><code> &lt;?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']) &amp;&amp; $_POST['session'] == "start" &amp;&amp; $_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 &lt; count($_SESSION['selection_list']); $counter++) { $str .= $_SESSION['selection_list'][$counter] . "&lt;br /&gt;"; } //echo $str; echo count($_SESSION['selection_list']); } if (isset($_POST['session']) &amp;&amp; $_POST['session'] == "get" &amp;&amp; count($_SESSION['selection_list']) &gt; 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']) &amp;&amp; $_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"; } } ?&gt; </code></pre> <p>The ajax is wrking well, passes data correctly, and displays data correctly. The only problem is with cookies.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload