简体   繁体   中英

PHP Using a Session cookie ID set by 3rd party server/URL

I am building a WordPress shopping cart plugin that will tie into our server through a URL that will produce an XML page, which I will then have to parse the info to the WordPress page.

The server contains a session id (ShoppingCart.SessionID) that I need to get and use to help store which button was clicked and then pull the product info and add it to the cart. Each button is given a product id via shortcode (product_id="some#") from the page owner and that ID is pulled from the server to display the product's info.

How the buttons are set up:

In the shortcode file, I am using a function and have the button set up as

<?php
add_shortcode('add_cart_button', 'add_cart_button_handler');

function add_cart_button_handler($atts) {
    extract(shortcode_atts(array(
        'product_id' => '',
    ), $atts));
    return print_add_cart_button_for_product($product_id, $atts);
}

In the main plugin file:

This file handles the printing of the button and the function to display the cart

<?php
session_id();
session_start();

// This function prints the button with its assigned $product_id

function print_add_cart_button_for_product($product_id, $atts = array()) {
    $replacement .= '<form method="POST" class="cart-button-form" style="display:inline" action="">';
    $replacement .= '<input type="hidden" name="bmt_cart_product" value="' . $product_id . '" />';

    $replacement .= '</form>';
    $replacement .= '</div>';
    return $replacement;
}

Here is the function for what is supposed to be displaying the cart/product info:

function show_shopping_cart_handler($atts) {
    $output = "";
    $form = '';
    $output .= '<table style="width: 100%;">';

    $output .= '<tr class="cart_item_row">
        <th class="cart_item_name_th">' . (__("Item Name", "wordpress-simple-paypal-shopping-cart")) . '</th><th class="cart_price_th">' . (__("Price", "wordpress-simple-paypal-shopping-cart")) . '</th><th></th>
        </tr>';

# For now, I am using this to print the headers that display the
# ShoppingCart.SessionID="somevalue", but on every button click it changes the value of the session id.

    if (isset($_POST['add_cart_submit'])) {

// The server reads the ID from "&PRODUCTID=product#"
            $id = "&PRODUCTID=" . $_POST['cart_product'];
            $url = "https://secure.bmtmicro.com/cart?CID=2/WP" . $id;
            $options = array(
                'http' => array(
                    'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method' => 'GET',
                    'content' => http_build_query($data)
                )
            );
            //$context = stream_context_create($options);
            $context = stream_context_set_default($options);
            $output .= file_get_contents($url, false, $context);

        }
        print_r(get_headers($url));

       $output .= '</table>';
       return $output;
}

What the XML page looks like with an example product

Not sure if this is needed, but the more info provided, the better (right?). The product ID that is sent will populate these fields and keep adding a new row and then I will parse the necessary fields to the WordPress page.

<shoppingcart sessionid="88582813">
  <producttable>
   <row number="0">
    <productid>22804</productid>
    <productname>XFree86 CD</productname>
    <quantity>1</quantity>
    <productprice>$15.00</productprice>
    <discount>$0.00</discount>
    <rowtotal>$15.00</rowtotal>
   </row>
  </producttable>
 </shoppingcart>

I am pretty new to PHP and learning as I go (reading/research), but have not been able to find the solution or steps to tackle my issue. Essentially, I believe my question should be (in short):

How can I grab the ShoppingCart.SessionID from the 3rd party server and use it to populate the cart without refreshing the Session so more than one product will display when more than one button is clicked? Also, is there a better/more productive way to accomplish what I am trying to do?

Thanks in advance and let me know if there's any info I need to add to the question!

In case anyone comes across this with a similar issue:

My way around this was to send the button data to an <iframe> and then set the <iframe> to width="0" height="0" .

Example code:

<form method="POST" action="URL" target="the_iframe">
  // add some <input> fields data
</form>

<iframe type="hidden" name="the_iframe" width="0" height="0"></iframe>

Not sure if this is THE BEST way to do it, but it works and I was able to add some JQuery to manipulate things even further (I tried using AJAX before this, but the server was set to "same origin", so the data wouldn't send from another URL).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM