-2

i have created a shopping cart in php with implementing session but when the user clicks on the add-to-cart button on detail page on server it does not redirects to cart-page.php and it works fine on localhost.i think header function is not properly working please help me to find out the problem.

here is my code.

enter code here

    <div id="central_content"> 
   <?php

    include("include/db.php");
    include("include/functions.php");
    error_reporting(E_ALL);
ini_set("display_errors", 1);
    if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' && $_REQUEST['productid']>0){
        $pid=$_REQUEST['productid'];
        addtocart($pid,1);
        session_start();
        if($_SESSION['login']=="loggedin")
        {
        header('location:cart-page.php');
        }
        else
        {

            ?>
            <script>
        document.getElementById("logindiv").innerHTML="welcome";
            </script>

        <?php 
        header('location:login-page.php');
                }
        exit();}
?>
<script language="javascript">
    function addtocart(pid){
        document.form1.productid.value=pid;
        document.form1.command.value='add';
        document.form1.submit();
    }
</script>


<body>
<form name="form1">
    <input type="hidden" name="productid" />
    <input type="hidden" name="command" />
</form>
<div align="center">
    <h1 align="center">Henger2</h1>
    <table border="0" cellpadding="2px" width="600px">
        <?php

            $result=mysql_query("SELECT * FROM `products` WHERE serial=1");
            while($row=mysql_fetch_array($result)){
        ?>
        <tr>
            <td></td>


            <td> <div class="product_item"> <div class="product_left"> <a href="<?=$row['picture']?>" class="simple_image" title=""><span class="product_zoom" style="display: none;"><img src="styles/Images/ico-zoom.png" alt="Zoom Picture" title="Zoom Picture" style="border:none;"/></span><img src="<?=$row['picture']?>" alt="Image 1" title=""/></a>
            <div class="product_thumb_container">  
            </div>
          </div>
           </div>
          </td>

              <td> 

              <div class="product_right">

          <div class="product_data">Product:</div>
          <div class="product_data grey"><?=$row['name']?></div>
          <div class="div_br"></div>
          <div class="product_data">Product-Softwares:</div>
          <div class="product_data grey"><?=$row['description']?></div>
          <div class="div_br"></div>
          <div class="product_data">Software-Format:</div>
          <div class="product_data grey"><?=$row['desc']?></div>
          <div class="div_br"></div>
          <div class="product_data">Price:</div>
          <div class="product_data grey">$<?=$row['price']?></div>
          <div class="div_br"></div>    


           <input type="submit" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />

            </td>
        </tr>
        <tr><td colspan="2"></td>
        <?php } ?>
    </table>
</div>

             cart-page.php




     <script language="javascript">
    function del(pid){
        if(confirm('Do you really mean to delete this item')){
            document.form1.pid.value=pid;
            document.form1.command.value='delete';
            document.form1.submit();
        }
    }
    function clear_cart(){
        if(confirm('This will empty your shopping cart, continue?')){
            document.form1.command.value='clear';
            document.form1.submit();
        }
    }
    function update_cart(){
        document.form1.command.value='update';
        document.form1.submit();
    }


</script>
 <?php

    include("include/db.php");
    include("include/functions.php");

    if(isset($_REQUEST['command']) && $_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
        remove_product($_REQUEST['pid']);
    }
    else if(isset($_REQUEST['command']) && $_REQUEST['command']=='clear'){
        unset($_SESSION['cart']);
    }
    else if(isset($_REQUEST['command']) && $_REQUEST['command']=='update'){
        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=intval($_REQUEST['product'.$pid]);
            if($q>0 && $q<=999){
                $_SESSION['cart'][$i]['qty']=$q;
            }
            else{
                $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
            }
        }
    }


?>
<script language="javascript">
    function del(pid){
        if(confirm('Do you really mean to delete this item')){
            document.form1.pid.value=pid;
            document.form1.command.value='delete';
            document.form1.submit();
        }
    }
    function clear_cart(){
        if(confirm('This will empty your shopping cart, continue?')){
            document.form1.command.value='clear';
            document.form1.submit();
        }
    }
    function update_cart(){
        document.form1.command.value='update';
        document.form1.submit();
    }


</script>

<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
    <div style="margin:0px auto; width:600px;" >
    <div style="padding-bottom:10px">
        <h1 align="center">Your Shopping Cart</h1>
    <input type="button" value="Continue Shopping" onclick="window.location='projects.php'" />
    </div>
        <div style="color:#F00"></div>
        <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
        <?php

            if(is_array($_SESSION['cart'])){
                echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
                $max=count($_SESSION['cart']);
                for($i=0;$i<$max;$i++){
                    $pid=$_SESSION['cart'][$i]['productid'];
                    $q=$_SESSION['cart'][$i]['qty'];
                    $pname=get_product_name($pid);
                    if($q==0) continue;
            ?>
                    <tr bgcolor="#FFFFFF"><td><?=$i+1?></td><td><?=$pname?></td>
                    <td>$ <?=get_price($pid)?></td>
                    <td><input type="text" name="product<?=$pid?>" value="<?=$q?>" maxlength="3" size="2" /></td>                    
                    <td>$ <?=get_price($pid)*$q?></td>
                    <td><a href="javascript:del(<?=$pid?>)">Remove</a></td></tr>
            <?php                   
                }
            ?>
                <tr><td><b>Order Total: $<?=get_order_total()?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()">



                </td></tr>
            <?php
            }
            else{
                echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
            }
        ?>
        </table>
    </div>
</form>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
 <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" style="margin-left:500px; margin-top:20px;">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="pankaj.gargas@gmail.com">
<?php
            if(is_array($_SESSION['cart'])){
                $max=count($_SESSION['cart']);
                $a=1;

                foreach($_SESSION['cart'] as $item){
                $name=get_product_name($a); 


            ?>
<input type="hidden" name="item_name_<?php echo $a; ?>" value="<?=$name; ?>">
<input type="hidden" name="quantity_<?php echo $a; ?>" value="<?= $item['qty']; ?>">
<input type="hidden" name="amount_<?php echo $a; ?>" value="$ <?=get_price($a)?>">
<?php 
$a++;
}
            }?>


</form>
sakshi
  • 1
  • 1

1 Answers1

0

This code is invalid. There are a session_start(), and redirect, but there was output before: <div id="central_content">.

I assume, there was <html> and other HTML tags before.

Move your these code befor you make ANY output.

And after header('location ...'); add a die();

vaso123
  • 12,347
  • 4
  • 34
  • 64
  • this code is working properly on localhost. but on server when add-to-cart button is clicked on product-page.php it redirects to http://allsolution.co/product-page.php?productid=1&command=add – sakshi Nov 07 '14 at 10:57
  • Working properly on localhost does not means, this is a valide code. – vaso123 Nov 07 '14 at 11:03
  • The problem is what i mentioned in my answer. This whole things happens, because when you click on that button, then you send the form 1 with 2 parameters: productid and command, and not defined the action for form, so it will send it with get method to itselfs. Since here is everythin ok. But, because you alread has output before your session start, and header, header won't happens on production server. – vaso123 Nov 07 '14 at 11:05
  • then how do i change it.please mention it with line number. – sakshi Nov 07 '14 at 11:11
  • Move your whole `include('db.php')`; until `addtocart` javascript function code block before ANY output in the buffer. So, berore ` ` – vaso123 Nov 07 '14 at 11:16
  • this also does not work on server. i also having this kind of session problem in other project also – sakshi Nov 08 '14 at 06:56
  • please answer this. i had moved include('db.php'); outside of – sakshi Nov 08 '14 at 10:05