php - Wordpress Ajax setcookie WAMP/LAMP disparities -
in wordpress themed-plugin e-commerce have 2 ajax/php scripts setting cookies (both in same directory) ajax calls set same "cart.js" first script sets/updates anonymous cart cookie when cart either created or updated. second checks if customer/user exists, or creates 1 anew , --in either case -- logs them in if not already, before cart gets passed paypal. such, upon returning paypal customer/user (now logged in) presented overview / review of status of orders (new , old).
on wamp develpment stack, works flawlessly, while on hosted (linux) installation cart_cookie script works expected, while 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 sent (output started @ /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, user not logged in, , cart not converted (updated relevant customer data). wish saw way pare down minimum, have no earthly idea why 2 scripts behave differently across platforms i'll apologize in advance lengthy post , include them both in entirety below, , ask if can see obvious reason disparity? patience.
p.s. both wamp , 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 $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 $key => $item )$amt_cart[] = $item; $amt_cart = serialize($amt_cart); } if($reset) { // initiated cart (tpl_cart.php on page load) remove out-of-stock items // old carts -- items have gone out-of-stock since cart created -- or // 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, , add, update, or remove cart-items within 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 $key => $item) { if($key == 0 ) $amt_cart_id = $item; else { foreach($item $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 $key => $val) $$key = $val; foreach($buyer $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 $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 $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 $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 self 'address2' => $buyer_address2,//null if not ship self 'city' => $buyer_city,//google guess if not ship self 'region' => $buyer_region,//google guess if not ship self 'postal_code' => $buyer_postal_code,//null if not ship self 'country' => $buyer_country,//google guess if not ship self 'ctry_code' => $buyer_ctry_code,//google guess if not ship 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 * {$wpfx}amt_carts 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); } ?>
this is, believe, due difference in php configuration. cannot tell more without knowing full list of post variables, besides broad guidelines.
this assume causing issue:
- you're assigning $_post[key] $key. fine
- you're looping through $buyers. if $_post["buyers"] not set or not array, throw notice.
- a notice echo. not in output buffering context, therefore causes headers sent. if headers sent , later send cookie or other header info, warning getting.
one of dev environments has error_reporting set e_none. check in php information. if set e_none, code work. if not, message. consider always checking existence , correct type of functions. way concisely follows:
foreach (((array)$buyers) $v) {
if $buyers undefined, empty array. if buyers had 1 element have array 1 element. otherwise, array had.
Comments
Post a Comment