我声明了一个变量$counter = 1;
,并将一个条件放入else
条件中。如果CCD_ 3。它应该重定向到header("Location:" . BASE_URL . "product.php");
但我得到了一个错误如下!
警告:无法修改标头信息-标头已由(输出起始于D:''wamp''www''project''ali''views''header.php:72)在D:''wamp ''www''project''ali ''products''check_out.php第125行发送
下面是check_out.php页面的代码!
<?php
require_once '../models/user.php';
require_once '../models/brand.php';
require_once '../models/cart.php';
//require_once '../models/item.php';
require_once '../views/top.php';
require_once '../models/place_order.php';
$counter = 1; // initial value
?>
</head>
<body>
<?php
if (!$obj_user->login) {
//redirect to the sign up page e.g
header("Location:" . BASE_URL . "signup.php");
}
?>
<div id="wrapper">
<?php
require_once '../views/header.php';
?>
<!-- **************************Main************************************ -->
<div id="main">
<div id="col-main">
<?php
require_once '../views/middle_left.php';
?>
<div id="middle-right">
<h3 class="text-center">Check Out</h3><hr>
<div class="row">
<div class="col-lg-12">
<h4>Billing Information</h4>
<span>Welcome <?php echo($obj_user->full_name); ?> Online Computer Shop</span>
</div>
</div>
<hr>
<div class="row">
<div class="col-lg-12">
<form class= "form-horizontal" action='" . BASE_URL . "products/process/process_cart.php' method='post'>
<div class="form-group">
<label class="col-sm-2 control-label" for="billing_name">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="billing_name" name="billing_name" value="" placeholder="Enter Name" >
<span id="billing_name_error">
<?php
if (isset($errors['billing_name'])) {
echo ($errors['billing_name']);
}
?>
</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="billing_address">Address</label>
<div class="col-sm-10">
<textarea class="form-control" id="billing_address" name="billing_address" value="" placeholder="Address"></textarea>
<span id="billing_address_error">
<?php
if (isset($errors['billing_address'])) {
echo ($errors['billing_address']);
}
?>
</span>
</div>
</div>
</form>
</div>
</div>
<hr>
<br>
<div class="shopping-background">
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<?php
if ($obj_cart->items)
{
echo("<form action='" . BASE_URL . "products/process/process_cart.php' method='post'>"
. "<table class='table table-hover' width='400px' border='1px' bordercolor='black' cellspacing='0' cellpadding='0'>");
echo("<thead>"
. "<tr align='center'>"
. "<th>Product Count</th>"
. "<th>Product Name</th>"
. "<th>Quantity</th>"
. "<th>TOTAL</th>"
. "</tr>"
. "</thead>");
foreach ($obj_cart->items as $item) {
echo("<tbody>"
. "<tr align='center'>"
. "<td>$counter</td>"
. "<td>$item->item_name</td>"
. "<td>$item->quantity</td>"
// . "<td><input class='box' type='text' value='$item->quantity' name='qtys[$item->itemID]'></td>"
. "<td>$item->total_price</td>"
. "</tr>"
. "</tbody>");
$counter++; // update counter value on each product
}
echo("<thead>"
. "<tr align='right' valign='middle'>"
. "<th></th>"
. "<th></th>"
. "<th></th>"
. "<th align='center'>$obj_cart->total_price</th>"
. "</tr>"
. "</thead>");
echo("</table></form>");
} else {
$counter == 0;
//echo("<label>Your cart is empty</label>");
header("Location:" . BASE_URL . "product.php");
}
?>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
</div>
<hr>
<div class="row">
<div class="col-sm-3 col-sm-offset-2">
<a href="<?php echo(BASE_URL); ?>products/products.php"><input class='btn btn-default' type='button' value='Shop More' /></a>
</div>
<div class="col-sm-3 col-sm-offset-2">
<a href="<?php echo(BASE_URL); ?>"><input class='btn btn-default' type='button' value='Place Order' /></a>
</div>
<hr>
</div>
<br><br>
</div>
</div>
</div>
</div>
<!--***************************End of Main************************************ -->
<!-- ***************************Footer************************************ -->
<?php
require_once '../views/footer.php';
?>
<!-- *************************Footer************************************** -->
</body>
</html>
如何修复"标头已发送";PHP 中的错误
基本上,将您的输出存储在某个变量中,只有在您知道没有重定向后才将其回显。http://php.net/manual/en/function.ob-get-contents.php如果你想制作自己的模板系统,可能也很有用。
根据PHP手册
在发送任何实际输出之前,必须调用header()普通的HTML标记、文件中的空行或来自PHP。
而是使用
<?php
if (!$obj_user->login) {
//redirect to the sign up page e.g
echo "<script>location.href='" . BASE_URL . "signup.php'</script>";
}
?>
其他部分:
else {
$counter == 0;
//echo("<label>Your cart is empty</label>");
echo "<script>location.href='" . BASE_URL . "product.php'</script>";
}
或者你可以使用:
ob_start()
你可能想读这本书。http://php.net/manual/en/function.ob-start.php
您可以使用此<script>location.href="<?php echo BASE_URL.'product.php' ?>";</script>
而不是标头进行重定向("Location:".BASE_URL."product.php");