Connecting Tech Pros Worldwide Forums | Help | Site Map

Cannot modify header information

Member
 
Join Date: Aug 2007
Posts: 120
#1: Nov 12 '07
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\phoenixhrsolutions\login.php:8) in C:\wamp\www\phoenixhrsolutions\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's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#2: Nov 12 '07

re: Cannot modify header information


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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   echo "Hello";
  3.   header("Location: http://www.google.com");
  4. ?>
  5.  
While this will send you to Google.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   header("Location: http://www.google.com");
  3.   echo "Hello";
  4. ?>
  5.  
Check out the Output Control Functions for a simple way to control your output.
Member
 
Join Date: Aug 2007
Posts: 120
#3: Nov 13 '07

re: Cannot modify header information


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).
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. include_once("functions.php");
  5. $cxn = connect_to_db("vars.php");
  6. ?>
  7. <html>
  8. <head>
  9. <script language='javascript'>
  10.   function load() {
  11.   var formObj = document.loginFrm.username;
  12.   formObj.focus();
  13. }
  14. </script>
  15. <title>Phoenix HR Solutions Login page</title>
  16. <link rel="stylesheet" href="style.css">
  17. </head>
  18.   <body background=back.gif onload='load();'>
  19. <?php
  20. #echo date("d/m/Y");
  21. function confirmUser($username,$password)
  22. {
  23.  
  24. #$query = "Select username, password from Users where username='$username' and password='$password'";
  25.  
  26.  $query = "Select Name, Confm_Passwd from Recruiter_Profile where Name='$username' and Confm_Passwd ='$password'";
  27.  
  28.  
  29.     #echo $query;
  30.  
  31.     $result = mysql_query($query) or die("Cannot execute the query");
  32.          #echo $result;        
  33.     $row = mysql_num_rows($result);
  34.  
  35.     #echo $row;
  36.     if(mysql_num_rows($result) != 1)
  37.  
  38.     return false; // 1 username exists
  39.  
  40.     else 
  41.  
  42.     return true;
  43.  
  44. }
  45. ?>
  46. <table bgcolor="#EDEDED" align="center" width=800 height=600 border=0 >
  47. <tr width=100% height=5%  cellpadding=0><td colspan=8 background="sidebar.gif" width=10% height=5%  class="ttitle"><div id="tttitle1"><img src="logo1.gif" ></img></div><div>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;PHOENIX HR SOLUTIONS&nbsp;&nbsp;&nbsp;&nbsp;</div> </td>
  48. </tr>
  49.  
  50. <tr><td>
  51. <table border='0'  height=20% cellspacing="8" cellpadding="8" align=center background=sidebar.gif class="MenuTable">
  52. <form method='post' name='loginFrm' action='login.php'>
  53. <br><br><br><br></br>
  54. <tr><td colspan=2 align=center class='field' style="font-family:verdana; font-size:12pt;">LOGIN</td></tr>
  55. <tr>
  56. <td class="Elementname"><b>User name</b></td><td><input type='text' size='20' name='username'></td></tr>
  57. <tr>
  58. <td class="Elementname"><b>Password</b></td><td><input type='password' size='21' name='pass'></td><br />
  59.  
  60.   <tr class=element><td colspan="2" align="center">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<input type='submit' name='submit' value='&nbsp;&nbsp;login&nbsp;&nbsp;'> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</td>
  61. </tr>
  62. </form>
  63. </td>
  64. </tr>
  65. <?php
  66. $username = $_POST['username'];
  67. $password = $_POST['pass'];
  68. $submit = $_POST['submit'];
  69.  
  70. #echo ($username);
  71. $crypt_pwd=md5($password);
  72. #echo ($crypt_pwd);
  73.  
  74. if(!empty($submit)){
  75.  
  76. if (!empty($username) && !empty($password)) {
  77.  
  78. #$cxn = mysql_connect("localhost","root","mysql");
  79.  
  80. #$db = mysql_select_db("phoenixhrdb",$cxn);
  81.  
  82. $auth = confirmUser($username,$crypt_pwd);
  83.  
  84. #echo "\n$auth";
  85.  
  86. if ($auth == "true") {
  87.  
  88.     $_SESSION['valid_user'] = $username;
  89. #    Write query ( select MAX(login_time) from logintimestamp where username='$username';
  90. #write the result into $lastlogin
  91. #        insert into logintimestamp values ($username, now(), NULL);
  92. #        :wq
  93.  
  94.       $time_query = "select MAX(logintime) from Logintimestamp where Username='$username'";
  95.     #echo $time_query;
  96.       $time_result = mysql_query($time_query,$cxn);
  97.       $time_row = mysql_fetch_array($time_result);
  98.       $lastlogin = $time_row['MAX(logintime)'];
  99.     #echo $lastlogin;
  100.       $_SESSION['login'] = $lastlogin;
  101.  
  102.     $insert_query = "INSERT INTO Logintimestamp values ('$username',now(),'NULL')";
  103.     # echo $insert_query;
  104.    $insert_result  =  mysql_query($insert_query,$cxn);
  105.  
  106. #            }
  107. if(true){
  108. $adminquery ="select Name from Recruiter_Profile where Team_Selection='Superuser' or Team_Selection='Management' or Team_selection='Manager'";
  109. $adminresult = mysql_query($adminquery) or die("Could not connect to database".mysql_error($cxn));
  110. #echo $adminresult;
  111.  
  112. while($adminrow = mysql_fetch_array($adminresult)){
  113. #echo $adminrow;
  114. $adminteam[]=$adminrow['Name'];
  115. }
  116. foreach($adminteam as $admin){
  117. #    echo "<br>$admin";
  118. #echo $username;
  119.        if($admin==$username){
  120. #    echo $admin;
  121.        header("Location:index.php");
  122.         exit;
  123. }
  124. else  if($admin !=$username){
  125. header("Location:indexcontinue.php");
  126.     exit;
  127. }
  128.     }}
  129. }
  130. else {
  131.  
  132.     echo ("<tr><td colspan='2'><b align='center'>Username or Password doesn't exist</b></td></tr>");
  133.     }
  134.  
  135. }
  136.  
  137.  
  138. else {
  139.  
  140.  
  141.     echo ("<tr><td colspan='2'><b>Please Enter user name and Password</b></td></tr>");
  142. }
  143.  
  144. }
  145. ?>      
  146. </table>
  147. </td></tr>
  148. <tr height=15><td  align=center background="sidebar.gif" colspan=8 cellpadding=0 cellspacing=0 class=footertxt>copyright2007 @ phoenixhrsolutions</td></tr>
  149. </table>
  150.            </body>
  151. </html>
  152.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#4: Nov 13 '07

re: Cannot modify header information


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:
Expand|Select|Wrap|Line Numbers
  1. echo "<script type=\"text/javascript\">location.href='http://www.google.com';</script>";
  2.  
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.
Member
 
Join Date: Aug 2007
Posts: 120
#5: Nov 14 '07

re: Cannot modify header information


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
Reply