Wordpress Ajax setcookie WAMP/LAMP差异 [英] Wordpress Ajax setcookie WAMP/LAMP disparities

查看:79
本文介绍了Wordpress Ajax setcookie WAMP/LAMP差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个用于电子商务的wordpress主题插件中,我有两个设置cookie的ajax/php脚本(都在同一目录).ajax调用是从相同的"cart.js"设置的创建或更新购物车时,第一个脚本设置/更新匿名购物车cookie.第二种方法检查客户/用户是否存在,或重新创建一个客户/用户,然后(无论哪种情况)将其登录(如果尚未存在),然后将购物车传递给PayPal.这样,从贝宝(Paypal)返回后,将向客户/用户(现在登录)显示其订单状态(新旧)的概览/评论.

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).

在我的WAMP开发堆栈中,在托管(linux)安装中它可以完美地工作 cart_cookie脚本按预期工作,而checkout/customer_cookie抛出...

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. WAMP和LAMP堆栈都运行php 5.2

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

cart_add.php (可同时使用WAMP和&& LAMP)

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 (在WAMP上有效,在LAMP上失败)

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

推荐答案

我相信,这是由于PHP配置不同所致.除了广泛的指导原则之外,我在不了解POST变量的完整列表的情况下无法告诉您更多信息.

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.

这是导致问题的原因:

  1. 您正在将$ _POST [key]分配给$ key.很好
  2. 然后,您将遍历$ buyers.如果未设置$ _POST ["buyers"]或数组,则将引发通知.
  3. 通知是回声.您不在输出缓冲上下文中,因此会导致发送标头.如果发送了标头,而您以后又发送了cookie或其他标头信息,则会收到警告.

您的一个开发环境可能将error_reporting设置为E_NONE.在PHP信息中检查此内容.如果设置为E_NONE,则您的代码将起作用.如果没有,您将收到该消息.考虑始终检查功能的存在和正确类型.简洁地做到这一点的一种好方法如下:

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) {

如果未定义$ buyers,则将得到一个空数组.如果买家拥有一个元素,那么您将拥有一个包含一个元素的数组.否则,将获得您拥有的阵列.

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.

这篇关于Wordpress Ajax setcookie WAMP/LAMP差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆