PHP中的头函数无法获取GET数据

时间:2014-04-25 02:58:52

标签: php url

大家好,我的代码有问题。我有一个简单的电子商务网站,我没有框架。该站点在我的本地PC上运行没有错误。但是当我在另一台PC上移动它时。

这是我愿望清单的过程。提交表单后,它将重定向到自己。该过程只是将项目移至购物车。

 //remove item from cart
if(isset($_GET['remove_id'])) {

    if(isset($_SESSION['member'])) {

        fn_delete_item($_GET['remove_id'], $_SESSION['member']);

    } else {

        foreach ($_SESSION['cart'] as $key => $subarray){ 

            if ($subarray['product_id'] == $_GET['remove_id']){ 
                unset($_SESSION['cart'][$key]); 
                break; 
            } 

        }

    }

    header('Location: ?page=yoga_product&product='.$_GET['remove_id'].'&product_id='.$_GET['remove_id']);

} 

//remove all items from cart

if(isset($_GET['removeall'])){

    if(isset($_SESSION['member'])) {

        fn_delete_all_item($_SESSION['member']);
        unset($_SESSION['cart']);  

    } else {

        unset($_SESSION['cart']);  

    }

    header('Location: ?page=yoga_product&product='.$_GET['removeall'].'&product_id='.$_GET['removeall']);

}

...... 


echo "<div style='overflow-y: scroll; height:200px;'>";

echo "<table border='0' style='font-size: 12px; width: 100%' cellpadding='5'>";
    echo "<tr>";
        echo "<td style='background-color: white; color: black; text-align: center'>Product ID</td>";
        echo "<td style='background-color: white; color: black; text-align: center'>Name</td>";
        //echo "<td style='background-color: white; color: black; text-align: center'>Price</td>";
        echo "<td style='background-color: white; color: black; text-align: center'>Sale Price</td>";
        echo "<td style='background-color: white; color: black; text-align: center'>Quantity</td>";
        echo "<td style='background-color: white; color: black; text-align: center'>Total</td>";
        echo "<td style='background-color: white; color: black; text-align: center'><a href='?page=yoga_product&product=".$_GET['product_id']."&removeall=".$_GET['product_id']."' class='hover'>Remove All</a></td>";
    echo "</tr>";

    $total = 0;

    if(isset($_SESSION['member'])) {

        $cart_data = fn_get_cart($_SESSION['member']);

        $total_cart = fn_count_cart($_SESSION['member']);

        while ($row = mysql_fetch_assoc($cart_data, MYSQL_ASSOC) ){

            $link = '?page=yoga_product&product='.$row['product_id'].'&product_id='.$row['product_id'];
            $update = '?page=yoga_product&product='.$row['product_id'].'&update_id='.$row['product_id'];
            $remove = '?page=yoga_product&product='.$row['product_id'].'&remove_id='.$row['product_id']; //this will be the generated URL for removing.

            $compute_total = $row['qty'] * $row['sale_price'];

            echo "<tr>";
                echo "<td style='text-align: center; background-color: gray; color: black'>".$row['product_id']."</td>";
                echo "<td style='text-align: left; background-color: gray; color: black'><a href='$link' style='color: blue; '>".$row['product_name']."</a></td>";
                //echo "<td style='text-align: right; background-color: gray; color: black; font-family: courier'>".number_format($row['price'],2)."</td>";
                echo "<td style='text-align: right; background-color: gray; color: black; font-family: courier'>".number_format($row['sale_price'],2)."</td>";
                echo "<td style='text-align: center; background-color: gray; color: black'>".$row['qty']."</td>";
                echo "<td style='text-align: right; background-color: gray; color: black; font-family: courier'>".number_format($compute_total,2)."</td>";
                echo "<td style='text-align: center; background-color: gray; color: black'>
                        <a href='$remove' class='hover'>Remove this?</a>
                      </td>";
            echo "</tr>";

            $total += ($row['sale_price'] * $row['qty']);

        }

        echo "</table>";

        echo "</div>";

        echo "<br />";

        echo "<div align='right'>";
            echo "<label>Total Items: ".$total_cart."</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            echo "<label>Subtotal Amount: </label><input type='text' name='subtotal' value='".number_format($total,2)."' readonly='readonly' style='text-align: right; font-family: courier'>";
            echo "<input type='submit' value='Place Item' disabled='disabled' />";
        echo "</div>";

    }

它将删除该项目,但我收到了错误。

Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\ahm\pages\main\menu.php:50) in E:\xampp\htdocs\ahm\pages\yoga\clothing_shop.php on line 179

我不知道我是怎么得到这个错误的。在我当地有这样的错误。但是当我把它移到其他PC上时。我懂了。我以这种方式访问​​我的页面。 10.x.x.x./ahm

请帮帮我们。感谢。

2 个答案:

答案 0 :(得分:1)

在您从$ _SESSION获取任何内容之前,您需要使用session_start()并在您在页面中打印任何内容之前执行此操作并且之后您也无法发送标题

在从$ _SESSION获取任何内容之前尝试使用,您需要使用session_start()并在页面中打印任何内容之前执行此操作

您还可以尝试使用try / catch来准确发现代码中出现的问题

答案 1 :(得分:1)

在调用header()函数之前,只要发送了任何类型的输出,就会发生此错误。 (或使用$ _SESSION)您可以使用exit()替换标题功能并查看页面来源。如果那里有任何类型的文本,您需要重新构建代码,以便之前不输出文本。

echo "hello";

header("location: ...blah blah");

导致:警告:无法修改标头信息 - 已在E:\ xampp \ htdocs \ ahm中发送的标头(在E:\ xampp \ htdocs \ ahm \ pages \ main \ menu.php:50处开始输出)第179页的\ pages \ yoga \ clothing_shop.php

了解header()函数的真正含义将极大地帮助您。简单地说,它告诉请求服务器期望什么样的数据。如果在该通知之前发送了任何类型的数据,则服务器会抱怨。