使用Post/Rerect/Get,但当页面被重定向回时,我仍然需要刷新


Using Post/Redirect/Get but when page gets re-directed back I still have to refresh

我有一个简单的Post/Rerect/Get功能,所以当用户将产品添加到他们的购物车时,当他们之后刷新页面时,它不会再添加一个产品。

这是我的代码:

    <?php
include("wfcart.php"); ?>
<?php session_start(); ?>
<?php $cart =& $_SESSION['cart'];
if(!is_object($cart)) $cart = new wfCart(); 
    $user_id = get_current_user_id();
    $query = mysql_query("SELECT SUM(value) FROM wp_scloyalty WHERE userid = '$user_id'");
    $totalpoints = mysql_result($query, 0);   
    $purchasevalue=array(); ?>
    <?php $sqlprize="SELECT prizeid FROM wp_scloyalty_orders WHERE  userid = '$user_id'";
    $resultprize=mysql_query($sqlprize); ?>
    <?php while($rows=mysql_fetch_array($resultprize)){  
            query_posts('post_type=prizes&showposts=-1&p='.$rows['prizeid'].''); while (have_posts()) : the_post(); 
            $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
            $purchasevalue[] = $my_meta['pointsvalue'];
            endwhile; 
            wp_reset_query();  
    }
    $calctotalnew = $totalpoints - array_sum($purchasevalue);

    if($_POST['purchase']) {
            foreach($cart->get_contents() as $item) {
                $sql="INSERT INTO wp_scloyalty_orders VALUES (".$user_id.", ".$item['id'].")";
                $result=mysql_query($sql);
            }
            $cart->empty_cart();
            unset($_SESSION['cart']);           
    }       
    $err = array();
    if($_POST['add']) {
        query_posts('post_type=prizes&showposts=-1&p='.$product_id.''); while (have_posts()) : the_post(); 
        $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
        if($calctotalnew > $my_meta['pointsvalue']){
            $cart->add_item(get_the_id(), 1, $my_meta['pointsvalue'], get_the_title());
        }else{  
            //echo 'You do not have sufficient points to redeem this product...';
            $err[] = "You do not have sufficient points to redeem this product..."; 
        }
        endwhile; 
        wp_reset_query();  
        if (!$err) {  
            // if no errors - saving data 
            // and then redirect:
            header("Location: ".$_SERVER['PHP_SELF']."/my-account");
            exit;
        }  else {
            // all field values should be escaped according to HTML standard
            foreach ($_POST as $key => $val) {
                $form[$key] = htmlspecialchars($val);
            }   
        }   
    }// else {
    //      $form['add'] = $form['add'] = '';  
    //}

?>

所以我的问题是,当我从购物车确认购买时,页面上有一个部分显示用户剩余资金。为了查看新的余额,他们必须在购买后刷新页面。

我只是想知道我是不是真的做错了什么。。。也许与标题有关:位置问题?我注意到,事情确实取决于不同$_POST的IF对账单的顺序,但似乎无法做到,所以当他们购买时,它会自动更新余额:/

谢谢!顺便说一句,很抱歉有大量的代码,只是不知道什么是重要的还是不重要的。

不确定这是否是答案,但我在网上找到了这个:

"header()用于发送特定于HTTP/1.1规范的原始头球在发送任何实际输出之前,必须调用header()以下示例将不起作用:"

现在我知道,在过去,这给我在Linux网络服务器上托管的网站带来了问题。在Windows托管的服务器上,我似乎对重定向代码有更多的回旋余地(它在HTML中的位置并没有那么挑剔)。

如果可以,请在基于Windows的服务器上试用该代码。如果它仍然存在问题,则可能不是标头。

此外,请尝试重定向到您正在使用的php页面(如example.php),而不是$_SERVER['HP_SELF']。这可能也会导致问题,因为代码试图执行额外的步骤,这可能会使事情变得一团糟。