2013-12-18 82 views
1

我已經爲wordpress創建了一個插件。 下面是一個示例代碼。該代碼運行良好,併成功將數據存儲在cookie中。但問題是當我刷新頁面時,舊數據丟失,並再次寫入相同的數據...WordPress的頁面丟失餅乾

下面的代碼塊在插件文件中。

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。

+0

嘗試接收響應的ajax文件位於同一個目錄中?例如:/dev/(anything-ajax.php)文件? – Shazzad

+0

最近我看到一個問題,Ajax調用不與主站點共享Cookie,我找到的原因是Ajax從Javascript調用,它們的路徑與普通瀏覽器不同。坦率地說,我沒有記得我是否在我的網站上解決了這個問題,但你可以朝這個方向進行搜索。 –

+0

剛剛爲你找到了一些東西:http://stackoverflow.com/questions/8854816/keeping-the-cookie-after-a-cross-domain-ajax-request –

回答

0

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

+0

感謝您的答覆。我嘗試過,但沒有奏效。 :-( –

+0

我需要看到一個更全球的背景,你能分享更多嗎? –

+0

感謝您對幫助我感興趣,我已經更新了上面的帖子。 –