-5

I am currently encountering a problem that keeps giving me the error on the question title. here is my code, can anyone help me check what is wrong and how to fix it? Thank you so much.

The following part is the bottom part of a php file including "Header":

echo'<table>';
            while ($result = mysql_fetch_array($sql, MYSQL_ASSOC)){
                echo '<tr>';
                echo '<td style="width:142px">';
                echo $result['instrument_id'];
                echo '</td>';
                echo '<td style="width:142px">';
                echo $result['instrument'];
                echo '</td>';
                echo '<td style="width:142px">';
                echo $result['type'];
                echo '<br>';
                echo $result['amount'].'&nbsp;shares';
                echo '</td>';
                echo '<td style="width:142px">';
                echo $result['opening_rate'];
                echo '<br>';
                $fp = fopen ("http://finance.yahoo.com/d/quotes.csv?s='".$result['instrument']."'&f=nsb2b3p2ophg.csv","r");
                    //this uses the fgetcsv function to store the quote info in the array $data
                    $data = fgetcsv ($fp, 1000, ",");
                    echo ($data[3]);//current rate
                echo '</td>';
                echo '<td style="width:142px">';
                echo $result['opening_value'];
                echo '<br>';
                $value = $data[3] * $result['amount'];
                $currentvalue = number_format($value, 2, '.', '');
                echo $currentvalue;//current value
                echo '</td>';
                echo '<td style="width:142px">';
                echo $result['open_time'];
                echo '</td>';
                echo '<td style="width:142px">';
                if($data[3] == 0){
                echo '<input type="submit" name="close'.$result['instrument_id'].'" value="Close" disabled="disabled"/>';}
                else{
                echo '<input type="submit" name="close'.$result['instrument_id'].'" value="Close"/>';}
                echo '</td>';
                echo '</tr>';

                fclose ($fp);

                $name = 'close'.$result['instrument_id'];
                $close = isset($_POST[$name]);
                if($close){

                    $instrumentid = $result['instrument_id'];
                    $_SESSION['instrumentid'] = $instrumentid;


                    $instrument = $result['instrument'];
                    $_SESSION['instrument'] = $instrument;

                    $amount = $result['amount'];
                    $_SESSION['amount'] = $amount;

                    header("Location:order_sell.php");
                }   
            }
            echo'</table>'; 

When I include a file on the top, which is the background file, only including html elements, it will give me the error message like Warning: Cannot modify header information ... here is the code for the included file:

<?php 
session_start();

    // checks if $_SESSION['userid'] exists
    $areYouLoggedIn = isset($_SESSION['username']);
?>
<!DOCTYPE html>
<html>
<head>

<style type="text/css">

/* background*/
body { background: url(images/background2.png) center center fixed no-repeat; 
webkit-background-size:cover;
moz-bacground-size:cover;
-o-background-size:cover;
background-size:cover;}

/* top menu layout*/
.Topmenu{
width:1000px;
background: #00AEEF;
border-radius: 3px 3px 3px 3px;
}

/* top menu cell layout*/
.Tabcells{ width:200px; 
text-align:center;
}

/* top menu cell text layout*/

/* top menu cell text layout*/
.Tabtexteffect
{ text-decoration: none;
color:white;
font-family:Arial;
font-size:15px;
}

.Tabtexteffect:hover
{
text-decoration: none;
color:white;
font-family:Arial;
font-size:20px;
}

.Paragraph {text-align:justify; 
text-justify:distributed-all-lines;
text-align-last:justify; 
font-family:arial; 
font-size:12pt;
width:1000px;
}

.Box {
width:950px;
text-align:left;
background: -webkit-linear-gradient(90deg, #F0E68C, #FFFFE0) repeat scroll 0 0 transparent;
background: -moz-linear-gradient(90deg, #F0E68C, #FFFFE0) repeat scroll 0 0 transparent;
border: 1px solid #AAAAAA;
border-radius: 10px 10px 10px 10px;
box-shadow: 0 0 15px #AAAAAA;
}

.Box2 {
    width: 200px;
    text-align: left;
    background: -webkit-linear-gradient(90deg, #FFFA8C, #FFFFE0) repeat scroll 0 0 transparent;
    background: -moz-linear-gradient(90deg, #FFFA8C, #FFFFE0) repeat scroll 0 0 transparent;
    border: 1px solid #AAAAAA;
    border-radius: 10px 10px 10px 10px;
    box-shadow: 0 0 15px #AAAAAA;
}


.Button {
background: -moz-linear-gradient(90deg, #0459B7, #08ADFF) repeat scroll 0 0 transparent;
background: -webkit-linear-gradient(90deg, #0459B7, #08ADFF) repeat scroll 0 0 transparent;
border: 1px solid #093C75;
border-radius: 3px 3px 3px 3px;
box-shadow: 0 1px 0 #FFFFFF;
color: #FFFFFF;
cursor: pointer;
font-family: Arial,sans-serif;
font-size: 12px;
font-weight: bold;
margin-right: -16px;
margin-top: 16px;
padding: 5px 10px;
text-decoration: none;
text-shadow: 0 1px 1px #333333;
text-transform: uppercase;
}




</style>


</head>
<body>
<form action="HomePage.php" method="POST">
<div style="width:1000px; margin:0 auto; min-height:100%;">
<div>
<a href="HomePage.php"><img style="width:25%;" src="images/Mock Apprentice.png" alt="Mock Apprentice"></img></a>
<?php 
if($areYouLoggedIn==false){
    echo'<table cellpadding="0" align="right" style="position: absolute; left: 940px; top: 15px;">';
    echo'<tr>';
    echo'<td width="130" align="center"><font face="Arial" size="3"><a href="Login.php"><input type="button" class="Button" value="Sign In""/></a></font></td>';
    echo'<td width="1" bgcolor=black><BR></td>';
    echo'<td width="100" align="center"><font face="Arial" size="3"><a href="Registration.php"><input type="button" class="Button" value="Sign Up""/></a></font></td>';
    echo'</tr>';
    echo'</table>';
}
if($areYouLoggedIn == true){
    echo'<table cellpadding="0" align="right" style="position: absolute; left: 950px; top: 40px;">';
    echo'<tr>';
    echo'<td width="130" align="center"><font face="Arial" size="4" color="#00AEFF">Welcome';
    echo'&nbsp';
    echo'<font color="#FFD700">';
    echo($_SESSION['username']);
    echo'</font>';
    echo'</font></td>';
    echo'<td width="1" bgcolor=black><BR></td>';
    echo'<td width="100" align="center"><font face="Arial" size="4"><a style="text-decoration: none; color:#00AEFF;" href="Logout.php">Logout</a></font></td>';
    echo'</tr>';
    echo'<table>';
}
?>
<HR color="#00AEEF">
</div>
<div>
<table width="1020" height="30" class="Topmenu">
<tr>
<td width="200" height="30" class="Tabcells"><a href="Intro.php" class="Tabtexteffect">Intro</a></td>
<td width="200" class="Tabcells"><a href="HowToUse.php" class="Tabtexteffect">How to use</a></td>
<td width="200" class="Tabcells"><a href="Elearning.php" class="Tabtexteffect">Education Center</a></td>
<td width="200" class="Tabcells"><a href="ITQuotes.php" class="Tabtexteffect">Trading Platform</a></td>
<td width="196" class="Tabcells"><a href="ContactUs.php" class="Tabtexteffect">Contact Us</a></td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>

Thank you so so so much!

user1616230
  • 9
  • 2
  • 6

5 Answers5

10

Try replacing header("Location:order_sell.php"); with

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=order_sell.php">';    
exit;
Shekhar
  • 910
  • 1
  • 13
  • 23
2

Whatever you output - being HTML only or output from PHP: Unless you do some tricking with the ob_*() functions you lose the ability to send headers the moment you output anything.

This is why they are called headers: In the HTTP protocol they preceed every single piece of payload body - including whitespace such as empty lines before a <?php tag.

The typical way to tackle this would be to regroup your logic as to first calculate what headers you need, then create your output, via echo(), printf(), include() or whatever.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
0

In PHP header functions must be called before any output is sent from the server. This includes echo calls as well as HTML info before the <?php ?> block.

petschekr
  • 1,293
  • 1
  • 13
  • 19
  • Thank you. Is there any other function I can use to replace the "header" function so that I can get rid of the error? – user1616230 Sep 03 '12 at 22:23
  • 1
    @user1616230: This is not a problem of the PHP `header()` function, but a property of the HTTP protocol: Headers go first. – Eugen Rieck Sep 03 '12 at 22:25
0

You can't send a header after any text has been sent to the browser:

header("Location:order_sell.php");

Needs to be before ANY text/output/whitespace is sent/echo'ed to the browser.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
0

When using header("Location:someLocation") be sure that you don't have any character outputted to the browser, otherwise you'll get the error Cannot modify header information. More on this at: http://php.net/manual/en/function.header.php