dongmeixi5311 2013-05-14 15:43
浏览 66

Wordpress Ajax setcookie WAMP / LAMP差异

In a wordpress themed-plugin for e-commerce I have two ajax/php scripts setting cookies (both in the same directory) The ajax calls are set from the same "cart.js" The first script sets/updates an anonymous cart cookie when a cart is either created or updated. The second checks if customer/user exists, or creates one anew and --in either case -- logs them in if not already, before the cart gets passed to PayPal. As such, upon returning from paypal the customer/user (now logged in) is presented with an overview / review of the status of their orders (new and old).

On my WAMP develpment stack, this works flawlessly, while on the hosted (linux) installation the cart_cookie script works as expected, while the checkout/customer_cookie throws...

[14-May-2013 02:08:50]
PHP Warning:  session_start()
    [<a href='function.session-start'>function.session-start</a>]:
        Cannot send session cache limiter - headers already sent
            (output started at /home2/alternam/public_html/demo/wp-content/themes/AM_Wallaby_Kids/checkout.php:2)
                in /home2/alternam/public_html/demo/wp-content/plugins/cat-man/catalog-manager.php on line 23

Subsequently, the user is NOT logged in, and the cart is not converted (updated with relevant customer data). Wish I saw a way to pare this down to a minimum, but as I have no earthly idea why the two scripts behave so differently across platforms I'll apologize in advance for the lengthy post and include them both in their entirety below, and ask if anyone can see some obvious reason for the disparity? Thanks for your patience.

P.S. Both WAMP and LAMP stacks running php 5.2

cart_add.php (works both WAMP && LAMP)

<?php
ob_start();
require_once(preg_replace("/wp-content.*/","wp-load.php",__FILE__));
ob_end_clean();
$ud_cart = $product_name = $product_url = $reset = "";
$_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);# Sanitize Post Input
foreach ($_POST as $key => $val)
{    if(!is_array($val)) $$key = html_entity_decode($val,ENT_QUOTES);
    else $$key = $val;
}
if($ud_cart)
{    $reset =1;
    $amt_cart[0] = $cart_id;
    if(@$items) foreach($items as $key => $item )$amt_cart[] = $item;
    $amt_cart = serialize($amt_cart);
}

if($reset)
{    //    Initiated from the cart (tpl_cart.php on page load) to remove out-of-stock items
    //    from OLD CARTS -- where items have gone out-of-stock since cart created -- OR
    //    to simply update/remove cart items upon user request (user clicks Update|Remove)
    if($amt_cart)
    {    $amt_cart = stripslashes($amt_cart);
        setcookie(AMART_CART, $amt_cart, time()+60*60*24*90, COOKIEPATH, COOKIE_DOMAIN);
    }
    exit;
}

// Create Cart, and add, update, or remove Cart-Items from within the catalog gallery && product detail pages
$add = array("product_id" => $product_id, "product_name" => $product_name, "product_type" => $product_type,"product_url" => $product_url,"qty" => $qty);
$update = "";
if( isset( $_COOKIE[AMART_CART] ) )
{    $amt_cart = stripslashes($_COOKIE[AMART_CART]);
    $amt_cart = unserialize($amt_cart);
    foreach($amt_cart as $key => $item)
    {    if($key == 0 ) $amt_cart_id = $item;
        else
        {    foreach($item as $attr => $value)
            {    if($product_id != $value) continue;
                else
                {    $update = 1;
                    if($qty == 0 )
                    {    unset($amt_cart[$key]);
                         break;
                    } else     $amt_cart[$key]['qty'] = $qty;
                }
            }
        }
    }
    if(!$update) $amt_cart[] = $add;
    setcookie(AMART_CART,  serialize($amt_cart), time()+60*60*24*90, COOKIEPATH, COOKIE_DOMAIN);
}
else
{    unset($_SESSION[STORE_ID]['dest_zip'], $_SESSION[STORE_ID]['dest_ctry']);
    $amt_cart[0] = uniqid(AMART_CART);
    $amt_cart[] = $add;
    setcookie(AMART_CART, serialize($amt_cart), time()+60*60*24*90, COOKIEPATH, COOKIE_DOMAIN);
}
?>

checkout.php (works on WAMP, fails on LAMP)

    <?php
// TPL CART POSTS VIA AJAX CALL IN cart.js
ob_start();
require_once(preg_replace("/wp-content.*/","wp-load.php",__FILE__));
ob_end_clean();

$_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
global $current_user, $wpfx;
$buyer_address1 = $buyer_address2 = $buyer_city = $buyer_region = $buyer_postal_code = $buyer_country = $buyer_ctry_code ="";

foreach ($_POST as $key => $val) $$key = $val;
foreach($buyer as $key => $val) $$key = $val;

$user_is_admin = current_user_can('manage_options');
if(!is_user_logged_in() || $user_is_admin )
{    if($userID = email_exists($email))
    {    $user_info = $user_info = get_userdata($userID);
        $user_login = $user_info->user_login;
        $display_name = $user_info->display_name;
        $welcome = "Welcome Back $display_name!";
    }
    if(@$welcome)
    {    if(!$user_is_admin )
        {    if(!$user_cnfm) die($welcome);
            $auth = get_object_vars(wp_authenticate($user_login, $user_pass));
            if(array_key_exists('errors',$auth)) die("Password Error");
            wp_set_auth_cookie( $userID, true);
            wp_set_current_user($userID, $user_login);
        }
        update_user_meta( $userID, 'customer', 1);
    }
    else
    {    $buyer_name = "$buyer_first $buyer_last";
        $ship_to_name = "$first_name $last_name";
        if($ship_to_self)
        {    foreach ( $ctry_opts as $key=>$value ) if (strcasecmp($country, $value) == 0) $buyer_country = $key;
            $buyer_address1 = $address1;
            $buyer_address2 = $address2;
            $buyer_city = $city;
            $buyer_region = $state;
            $buyer_postal_code = $zip;
            $buyer_ctry_code =strtolower($country);
        }    else foreach ( $ctry_opts as $key=>$value ) if ($buyer_ctry_code == $value) $buyer_country = $key;
        $userdata = $user_cookie = array(
            'user_login' => $email,
            'user_email'=> $email,
            'user_pass'=>$user_pass,
            'first_name'=>$buyer_first,
            'last_name'=>$buyer_last,
            'display_name' =>$buyer_name,
            'address1' => $buyer_address1,//null if not ship to self
            'address2' => $buyer_address2,//null if not ship to self
            'city' => $buyer_city,//google guess if not ship to self
            'region' => $buyer_region,//google guess if not ship to self
            'postal_code' => $buyer_postal_code,//null if not ship to self
            'country' => $buyer_country,//google guess if not ship to self
            'ctry_code' => $buyer_ctry_code,//google guess if not ship to self
            'customer' => '1'
        );
        $userID = wp_insert_user( $userdata );
        if(!$user_is_admin)
        {    wp_set_auth_cookie( $userID, true);
            wp_set_current_user($userID, $email);
        }
        unset($user_cookie['user_login'],$user_cookie['user_pass'],$user_cookie['display_name']);
        setcookie('AMART_CUSTOMER', serialize($user_cookie), time()+60*60*24*180, COOKIEPATH, COOKIE_DOMAIN);
    }
}
if(is_user_logged_in())
{    if(!$user_is_admin) $userID = $current_user->ID;
    $cart_id = $item_name;
    $cart = $wpdb->get_row("SELECT * FROM {$wpfx}amt_carts WHERE cart_id = '$cart_id'", ARRAY_A);
    if( $cart['host_checkout'] && isset($store_options->paypal_live) && $store_options->paypal_live !=='false')
        $host_checkout = true;
    $ship_to = serialize( array('first_name' => $first_name,'last_name' => $last_name,'address1' => $address1,'address2' => $address2,'city' => $city,    'state' => $state,'postal_code' => $zip,'country' =>$country));
    $attributes = array('ship_to' => $ship_to, 'customer_id'=>$userID, 'checkout_date'=>$now);
    $where = array('cart_id' => $cart_id);
    $wpdb->update("{$wpfx}amt_carts", $attributes,$where);
}
?>
  • 写回答

2条回答 默认 最新

  • drd43058 2013-05-14 15:49
    关注

    This is, I believe, due to a difference in PHP configuration. I cannot tell you more without knowing the full list of POST variables, besides broad guidelines.

    This is what I assume is causing the issue:

    1. You're assigning $_POST[key] to $key. This is fine
    2. You're then looping through $buyers. If $_POST["buyers"] was not set or not an array, this will throw a notice.
    3. A notice is an echo. You are not in an output buffering context, which therefore causes the headers to also be sent. If the headers are sent and you later send a cookie or other header info, you get the warning you are getting.

    One of your dev environments probably has error_reporting set to E_NONE. Check this in the PHP information. If set to E_NONE, your code will work. If not, you will get that message. Consider always checking for the existence and correct type of your functions. A good way to do so concisely is as follows:

    foreach (((array)$buyers) as $v) {
    

    if $buyers is undefined, you will get an empty array. If buyers had one element you will have an array with one element. Otherwise, you will get the array you had.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line