Cannot modify header information

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sbettadpur
    New Member
    • Aug 2007
    • 121

    Cannot modify header information

    hello everybody

    i am getting this type of error(shown below) after submitting the button in IE
    browser, but the same code is working on Firefox smoothly,

    Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\pho enixhrsolutions \login.php:8) in C:\wamp\www\pho enixhrsolutions \login.php on line 126

    i have database driven login authentication scripts. from the login page, if i enters username and password i am getting errors as shown above

    any body knows please send me the solution

    thank you
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    The Warning you are getting there is produced by you PHP server-side code, so it is very unlikely that it would have anything to do with which browser you are using...

    In any case. This warning is produced when you try to use the header() function after something has been printed. Once any content has been added to the response, the header information is locked an you can not modify it.

    For example, this here will produce this warning, and you won't be redirected:
    [code=php]
    <?php
    echo "Hello";
    header("Locatio n: http://www.google.com" );
    ?>
    [/code]
    While this will send you to Google.
    [code=php]
    <?php
    header("Locatio n: http://www.google.com" );
    echo "Hello";
    ?>
    [/code]

    Check out the Output Control Functions for a simple way to control your output.

    Comment

    • sbettadpur
      New Member
      • Aug 2007
      • 121

      #3
      Thanks a lot for ur reply, i am struggling very much from this issue,

      but still i have doubt, and i.e not cleared, below i sending my script please
      have a look, I will explain the script logic how the control is going.
      once user enters username and password, then that values will compare with the database tables, if username is valid then it will create one session variable,
      once session variable is set then that page is redirecting to index.php

      index.php contains and header.php and footer.php

      so, my question is where i have to put header information(i.e . redirection page).
      [code=php]
      <?php
      session_start() ;

      include_once("f unctions.php");
      $cxn = connect_to_db(" vars.php");
      ?>
      <html>
      <head>
      <script language='javas cript'>
      function load() {
      var formObj = document.loginF rm.username;
      formObj.focus() ;
      }
      </script>
      <title>Phoeni x HR Solutions Login page</title>
      <link rel="stylesheet " href="style.css ">
      </head>
      <body background=back .gif onload='load(); '>
      <?php
      #echo date("d/m/Y");
      function confirmUser($us ername,$passwor d)
      {

      #$query = "Select username, password from Users where username='$user name' and password='$pass word'";

      $query = "Select Name, Confm_Passwd from Recruiter_Profi le where Name='$username ' and Confm_Passwd ='$password'";


      #echo $query;

      $result = mysql_query($qu ery) or die("Cannot execute the query");
      #echo $result;
      $row = mysql_num_rows( $result);

      #echo $row;
      if(mysql_num_ro ws($result) != 1)

      return false; // 1 username exists

      else

      return true;

      }
      ?>
      <table bgcolor="#EDEDE D" align="center" width=800 height=600 border=0 >
      <tr width=100% height=5% cellpadding=0>< td colspan=8 background="sid ebar.gif" width=10% height=5% class="ttitle"> <div id="tttitle1">< img src="logo1.gif" ></img></div><div>&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;PHOENI X HR SOLUTIONS&nbsp; &nbsp;&nbsp;&nb sp;</div> </td>
      </tr>

      <tr><td>
      <table border='0' height=20% cellspacing="8" cellpadding="8" align=center background=side bar.gif class="MenuTabl e">
      <form method='post' name='loginFrm' action='login.p hp'>
      <br><br><br><br ></br>
      <tr><td colspan=2 align=center class='field' style="font-family:verdana; font-size:12pt;">LOG IN</td></tr>
      <tr>
      <td class="Elementn ame"><b>User name</b></td><td><input type='text' size='20' name='username' ></td></tr>
      <tr>
      <td class="Elementn ame"><b>Passwor d</b></td><td><input type='password' size='21' name='pass'></td><br />

      <tr class=element>< td colspan="2" align="center"> &nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;<in put type='submit' name='submit' value='&nbsp;&n bsp;login&nbsp; &nbsp;'> &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;</td>
      </tr>
      </form>
      </td>
      </tr>
      <?php
      $username = $_POST['username'];
      $password = $_POST['pass'];
      $submit = $_POST['submit'];

      #echo ($username);
      $crypt_pwd=md5( $password);
      #echo ($crypt_pwd);

      if(!empty($subm it)){

      if (!empty($userna me) && !empty($passwor d)) {

      #$cxn = mysql_connect(" localhost","roo t","mysql");

      #$db = mysql_select_db ("phoenixhrdb", $cxn);

      $auth = confirmUser($us ername,$crypt_p wd);

      #echo "\n$auth";

      if ($auth == "true") {

      $_SESSION['valid_user'] = $username;
      # Write query ( select MAX(login_time) from logintimestamp where username='$user name';
      #write the result into $lastlogin
      # insert into logintimestamp values ($username, now(), NULL);
      # :wq

      $time_query = "select MAX(logintime) from Logintimestamp where Username='$user name'";
      #echo $time_query;
      $time_result = mysql_query($ti me_query,$cxn);
      $time_row = mysql_fetch_arr ay($time_result );
      $lastlogin = $time_row['MAX(logintime) '];
      #echo $lastlogin;
      $_SESSION['login'] = $lastlogin;

      $insert_query = "INSERT INTO Logintimestamp values ('$username',no w(),'NULL')";
      # echo $insert_query;
      $insert_result = mysql_query($in sert_query,$cxn );

      # }
      if(true){
      $adminquery ="select Name from Recruiter_Profi le where Team_Selection= 'Superuser' or Team_Selection= 'Management' or Team_selection= 'Manager'";
      $adminresult = mysql_query($ad minquery) or die("Could not connect to database".mysql _error($cxn));
      #echo $adminresult;

      while($adminrow = mysql_fetch_arr ay($adminresult )){
      #echo $adminrow;
      $adminteam[]=$adminrow['Name'];
      }
      foreach($admint eam as $admin){
      # echo "<br>$admin ";
      #echo $username;
      if($admin==$use rname){
      # echo $admin;
      header("Locatio n:index.php");
      exit;
      }
      else if($admin !=$username){
      header("Locatio n:indexcontinue .php");
      exit;
      }
      }}
      }
      else {

      echo ("<tr><td colspan='2'><b align='center'> Username or Password doesn't exist</b></td></tr>");
      }

      }


      else {


      echo ("<tr><td colspan='2'><b> Please Enter user name and Password</b></td></tr>");
      }

      }
      ?>
      </table>
      </td></tr>
      <tr height=15><td align=center background="sid ebar.gif" colspan=8 cellpadding=0 cellspacing=0 class=footertxt >copyright200 7 @ phoenixhrsoluti ons</td></tr>
      </table>
      </body>
      </html>
      [/code]
      Last edited by Atli; Nov 13 '07, 11:58 AM. Reason: Added [code] tags.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        The header() function must be called before anything is added to response. So calling the function where you do will never work. Unless you use the Output Control Functions.

        A simpler example, in this case, would be to use Javascript to redirect the browser.
        There is of course always a risk using Javascript, as the client's browser can be set to ignore Javascript.
        You could try something like this, instead of your header call:
        [code=php]
        echo "<script type=\"text/javascript\">lo cation.href='ht tp://www.google.com' ;</script>";
        [/code]

        I would recommend, however, that you try to design all code, that requires the use of the header function, so that any calls the function are done before you print anything.

        Comment

        • sbettadpur
          New Member
          • Aug 2007
          • 121

          #5
          Thank you very much Mr.Atli

          my problem has solved, in any php script logic should come first then the display part has to come
          means in my script i am printing forms before callling header function i interchange as u said.

          thank you

          Comment

          Working...