Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Warning : Cannot modify header information - headers already sent by

Options
  • 11-09-2005 1:03am
    #1
    Registered Users Posts: 148 ✭✭


    Hi all,

    I kept getting this error message whenever I tried to use Header() function.

    Any idea what this error all about ?

    Thanks . . .


Comments

  • Closed Accounts Posts: 1,819 ✭✭✭K!LL!@N


    You can't output any html before you call the function.
    Post up the piece of code and i'll show you where the problem is.

    Killian


  • Registered Users Posts: 148 ✭✭maco


    Thanks Killian,

    Here is the code; Sorry it's a bit long . . .

    <table width="800" align="center" bordercolor="#CCCCCC">
    <tr bgcolor="#00CC00">
    <td colspan="3"> <div align="right"><font color="#FFFFFF" size="1" face="Georgia, Times New Roman, Times, serif">    <font face="Arial, Helvetica, sans-serif">Help  /  Site
    Map  / Contacts   </font></font></div></td>
    </tr>
    <tr bgcolor="#FFFFFF">
    <td colspan="3"><font color="#CCCCCC" size="1">g</font><font size="1"><br>
    <font color="#666699" size="6">xxxx/ Main</font><br>
    <font color="#CCCCCC">.</font></font></td>
    </tr>
    <tr bgcolor="#00CC00">
    <td height="30" colspan="3"> <div align="center"><font color="#999999" size="7" face="Georgia, Times New Roman, Times, serif"><font color="#336633" size="3"><font color="#999999" size="7" face="Georgia, Times New Roman, Times, serif"><font color="#336633" size="3"></font></font><font color="#FFFFFF">   <font size="2" face="Arial, Helvetica, sans-serif">Home  :  Services  : 
    Shopping  : Organisation   :  On Sale 
    :  Bulletin  :  <font size="7" face="Georgia, Times New Roman, Times, serif"><font size="3"><font size="2" face="Arial, Helvetica, sans-serif">
    Advance Search  </font></font></font> :  List or Edit Site 
    </font></font><font color="#FFFFFF" size="7" face="Georgia, Times New Roman, Times, serif"><font size="3"><font size="2" face="Arial, Helvetica, sans-serif">:</font></font></font><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">
    </font><font color="#999999" size="7" face="Georgia, Times New Roman, Times, serif"><font color="#336633" size="3"><font color="#FFFFFF"><font size="2" face="Arial, Helvetica, sans-serif">
     Site Promotion </font></font><font color="#FFFFFF" size="7" face="Georgia, Times New Roman, Times, serif"><font size="3"><font size="2" face="Arial, Helvetica, sans-serif"> </font></font></font><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">
    </font></font></font></font></font></div></td>
    </tr>
    <tr align="center" valign="middle">
    <td colspan="3"><div align="left"><font color="#00CC00" size="1"> <font face="Arial, Helvetica, sans-serif">  >
    Main > Home > </font></font></div></td>
    </tr>
    <tr align="center" valign="middle">
    <td colspan="3"><font color="#FFFFFF" size="1">.</font><font size="1"><br>
    </font><font size="3"><font color="#009900" face="Georgia, Times New Roman, Times, serif">         </font></font><font color="#006600" face="Georgia, Times New Roman, Times, serif"> </font><br>
    <font size="2">     </font> <div align="center"><font color="#6666FF" size="2" face="Arial, Helvetica, sans-serif"><strong><font size="1"><br>
    </font></strong><font size="1"> </font></font></div></td>
    </tr>
    <tr>
    <td width="150" rowspan="6" align="center" valign="top" background="images/bgr.png" bgcolor="#FFFFFF">
    <div align="center"><font color="#999999" size="7" face="Georgia, Times New Roman, Times, serif"><font color="#336633" size="3"><font color="#666666" size="2" face="Arial, Helvetica, sans-serif">
    </font></font></font></div>
     <br> </td>
    <td bgcolor="#FFFFFF"> <div align="center"><font color="#6666FF" size="2" face="Arial, Helvetica, sans-serif"><strong><font color="#6666FF" size="2" face="Arial, Helvetica, sans-serif"><font size="1"><em><strong><font color="#666699" size="2">Welcome
    to Town.ie</font></strong></em></font></font></strong></font></div></td>
    <td width="150" rowspan="6" background="images/bgr.png" bgcolor="#FFFFFF"> </td>
    </tr>
    <tr>
    <td bgcolor="#FFFFFF"><div align="center"><font size="1"><em><font color="#6666FF"><font face="Arial, Helvetica, sans-serif"><strong>
      </strong></font></font></em></font></div></td>
    </tr>
    <tr>
    <td height="63" align="center" valign="top"> <form action="" method="post" name="form1" onSubmit="MM_validateForm('emailid','','RisEmail','password','','R');return document.MM_returnValue">
    <table width="90%">
    <tr>
    <td valign="top"> <div align="right"> </div></td>
    <td>  </td>
    </tr>
    <tr>
    <td><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Email
    ID :</font></div></td>
    <td><input name="email" type="text" id="email" size="50" maxlength="50"></td>
    </tr>
    <tr>
    <td><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Password
    :</font></div></td>
    <td><input name="password" type="password" id="password" size="50" maxlength="50"></td>
    </tr>
    <tr>
    <td height="22"><font size="2" face="Arial, Helvetica, sans-serif"> </font></td>
    <td> </td>
    </tr>
    <tr>
    <td> </td>
    <td><input name="logon" type="submit" id="logon" value="logon"></td>
    </tr>
    <tr>
    <td> </td>
    <td> </td>
    </tr>
    <tr>
    <td><div align="right"> </div></td>
    <td> <div align="left">
    <?php

    $user = "rootenq";
    $host = "localhost";
    $password = "xxxxxx";
    $database = "yyyy";
    $connection = mysql_connect($host,$user,$password)
    or die ("Couldn't connect to server, please try again");
    $db = mysql_select_db($database, $connection)
    or die ("Couldn't connect to database, please try again");

    $emailkey = '';
    $pass = '';

    foreach ($HTTP_POST_VARS as $key => $value )
    {
    If ($key == "email")
    {
    $emailkey = $value;
    }
    If ($key == "password")
    {
    $pass = $value;
    }
    }
    if ($emailkey != '')
    {
    $query = "Select email from passpf where email = '$emailkey'";
    $result = mysql_query($query)
    or die ("Couldn't connect to database, please try again");
    $num = mysql_num_rows($result);
    if ($num == 0)
    {
    echo "Email ID $emailkey does not exist, please try again . . .";
    }
    elseif ($num == 1)
    {
    $query = "Select email from passpf where email = '$emailkey' and PASSWORD = $pass";
    $result = mysql_query($query)
    or die ("Couldn't connect to database, please try again");
    $num = mysql_num_rows($result);
    if ($num == 0)
    {
    echo "Invalid Password for Email ID $emailkey, please try again . . .";
    }
    elseif ($num == 1)
    {
    header ( "location : errordsp.php"); }
    }
    }
    ?>
    </div></td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    <tr>
    <td height="63" align="center" valign="top"> <p> </p>
    <p align="center"> </p></td>
    </tr>
    <tr>
    <td width="500" height="173" align="center" valign="top"> <p> </p>
    <p align="center"> </p></td>
    </tr>
    <tr bgcolor="#CCCCCC">
    <td height="21" bgcolor="#FFFFFF"> </td>
    </tr>
    <tr bgcolor="#00CC00">
    <td height="18" colspan="3"> <div align="center"><font color="#006600" size="1" face="Georgia, Times New Roman, Times, serif"><font color="#FFFFFF"><font face="Arial, Helvetica, sans-serif">Help  /  Site
    Map  / Contacts  </font></font></font><font color="#FFFFFF" size="1" face="Arial, Helvetica, sans-serif">/  Disclaimer</font><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif"><br>
    </font><font color="#FFFFFF" size="2" face="Geneva, Arial, Helvetica, sans-serif">
    </font></div></td>
    </tr>
    </table>


  • Closed Accounts Posts: 1,819 ✭✭✭K!LL!@N


    Move the PHP code to the top of the file before you output any HTML.
    It should be fine then.

    Killian


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    Bingo. If you plan to use the header() function you must start your file with <?php as the first 5 chars, and use the function before any print (or similar) statements, or before ?>


  • Registered Users Posts: 148 ✭✭maco


    Sorry Killian, I have moved it up already but still the same problem ...

    I used dreamweaver to create the page, does this mean I should use all PHP script to avoid this problem ?


  • Advertisement
  • Registered Users Posts: 148 ✭✭maco


    Sorry Civilian, not sure what you mean, I am very new to this . . .


  • Closed Accounts Posts: 1,819 ✭✭✭K!LL!@N


    [PHP]<?php

    //Put whatever PHP code you want here.

    //If some condition is meet then redirect the page.
    if( $foo == $bar )
    {
    header("Location: http://www.example.com/");
    exit;
    }
    ?>

    <!-- Put your HTML code here -->[/PHP]

    No worries, Maco.

    I'll try and explain it a little more.

    In the above piece of code, i've shown you basically what you need to do.

    Open a new file.
    Open the PHP tag <?php on the first line.
    There can't be anything before this, no spaces, nothing.

    Put in your code for connecting to the database or anything else you like.

    Then you can add your IF statement with the header() function inside that.
    Use exit; after you call the header() function to prevent anything else being executed.

    When you close the PHP tag, then you can output your HTML.

    Hope this explains it a little better.

    Killian


  • Registered Users Posts: 148 ✭✭maco


    Got you Killian, it worked now. Thanks you very much, spend long night yesterday just to fix that . .

    To recap, Header() function must be defined before any HTLM output, for what I am doing now I need a global variable so that I can code it anywhere in my file and then use that variable later to decide where to go . . .

    Thanks . . .


  • Closed Accounts Posts: 1,819 ✭✭✭K!LL!@N


    Glad you got it sorted in the end. :)

    Killian


  • Registered Users Posts: 148 ✭✭maco


    Killian one last question, how do I use global variables, I ahve been looking in the net and book for sample but still cannot find a good one.

    Is this possible;

    <?PHP

    echo $globalvariable;

    ?>

    HTML code here ...

    <?PHP

    $globalvariable = "test";

    ?>

    Thanks . . .


  • Advertisement
  • Closed Accounts Posts: 1,819 ✭✭✭K!LL!@N


    [PHP]
    <?php

    $myVariable = "I am a string variable, i like to be displayed on screen.";

    ?>

    <!-- Put some HTML here if you like -->

    <?php

    echo $myVariable;

    ?>
    [/PHP]

    Your code probably isn't working because when you try to echo the variable the first time it hasn't been initialised so there's nothing to echo out.

    In my example, i've initialised $myVariable with a string value.
    So when i echo it, you'll see that string outputted on the screen.

    Killian


  • Registered Users Posts: 148 ✭✭maco


    Thanks . . . I did not realise it that easy . . . thanks a million . .


  • Closed Accounts Posts: 1,819 ✭✭✭K!LL!@N


    :D

    Killian


Advertisement