0

I have declared a variable $counter = 1; and i have put a condition in else condition. if $counter= 0;. It should redirect to header("Location:" . BASE_URL . "product.php");

But i am getting a error as below!

Warning: Cannot modify header information - headers already sent by (output started at D:\wamp\www\project\ali\views\header.php:72) in D:\wamp\www\project\ali\products\check_out.php on line 125

Below is the code of check_out.php page!

<?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>
Ali Qayyum
  • 21
  • 8

3 Answers3

1

How to fix "Headers already sent" error in PHP

Basically, store your output in some variable and echo it out only after you know that you're not redirecting. http://php.net/manual/en/function.ob-get-contents.php might be of use as well if you want to make your own template system.

Community
  • 1
  • 1
  • right! but how to do here. can you suggest in code! what should i have edit on it – Ali Qayyum Dec 17 '15 at 04:58
  • It's probably going to be a big change. Judging by that you have some previous output on another page. So on that other output, at the very basic level, you'll want to store all of that html output in something like $output. Then literally any html before the header you'll want to append to that variable. Literally anything. A single white space will cause the error you have. Then, if you're not doing a header redirect, you can echo out $output. http://platesphp.com/ is a basic template system that might help you. You're probably going to have to rewrite some stuff. Sorry. :( – Charles Thompson Dec 17 '15 at 05:00
0

According to PHP Manual

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

Instead Use

<?php
    if (!$obj_user->login) {
        //redirect to the sign up page e.g
    echo "<script>location.href='" . BASE_URL . "signup.php'</script>";
    }
    ?>

and in else part:

else {
  $counter == 0;
  //echo("<label>Your cart is empty</label>");
  echo "<script>location.href='" . BASE_URL . "product.php'</script>";
}

or you can use:

ob_start()

and you might want to read this one. http://php.net/manual/en/function.ob-start.php

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
  • right! but in checkout page, i want to check as well. after login of user! if he/she has selected products in shopping cart or not. because after login the user. check out page is available for him/her. I wanted to put a condition in check out page. he must need to select product in shopping cart before access of check out. – Ali Qayyum Dec 17 '15 at 04:57
  • @AliQayyum So change there too: `echo "";` see updated answer – Thamilhan Dec 17 '15 at 05:00
0

You can redirect with this <script>location.href="<?php echo BASE_URL.'product.php' ?>";</script> instead of header("Location:" . BASE_URL . "product.php");

Learner
  • 347
  • 1
  • 5
  • 11
  • I have to add this on else condition below, syntax error – Ali Qayyum Dec 17 '15 at 05:00
  • first close php using this ?> and then write this and then start php – Learner Dec 17 '15 at 05:09