Hi all, I have an error that once user has submitted information the header could not enter information.

Warning: Cannot modify header information - headers already sent by (output started at F:\root\xampplite\htdocs\fyp\top.php:62) in F:\root\xampplite\htdocs\fyp\login.php on line 219

Heres my code

session_start();                                      
 include_once'top.php';
 include_once "config.php";
 
 //include_once "html.php";                           
 switch (@$_POST['do'])                                
 {
   case "login":    
                                      
     
     $sql = "SELECT LoginName FROM user
             WHERE LoginName='$_POST[fusername]'";     
     $result = mysqli_query($cxn,$sql)
               or die("Couldn't execute query.");      
     $num = mysqli_num_rows($result);                  
     if ($num > 0)  // login name was found            
     {
        $sql = "SELECT LoginName FROM user
                WHERE LoginName='$_POST[fusername]'
                AND password=md5('$_POST[fassword]')";
        $result2 = mysqli_query($cxn,$sql)
                   or die("Couldn't execute query 2.");
        $num2 = mysqli_num_rows($result2);
        if ($num2 > 0)  // password is correct        
        {
           $_SESSION['auth']="yes";                    
           $logname=$_POST['fusername'];
           $_SESSION['logname'] = $logname;            
           $today = date("Y-m-d h:i:s");               
           $sql = "INSERT INTO user (LoginName,CreateDate)
                   VALUES ('$logname','$today')";
           $result = mysqli_query($cxn,$sql)
                     or die("Can't execute insert query 1.");
           header("Location: member_page.php");        
        }
        else    // password is not correct            
        {
           $message="The Login Name, '$_POST[fusername]'
                     exists, but you have not entered the
                     correct password! Please try again.<br>";
           include("login_form.php");                  
        }
     }                                                 
     elseif ($num == 0)  // login name not found       
     {
        $message = "The Login Name you entered does not
                    exist! Please try again.<br>";
        include("login_form.php");
     }
   break;                                             

   case "new":
	  /* Check for blanks */                           
     foreach($_POST as $field => $value)               
     {
        if ($field != "fax")                          
        {
           if ($value == "")                           
           {
              $blanks[] = $field;
           }
        }
     }
     if(isset($blanks))                               
     {
        $message_new = "The following fields are blank.
	            Please enter the required information:  ";
        foreach($blanks as $value)
        {
           $message_new .= "$value, ";
        }
        extract($_POST);
        include("login_form.php");
		  include '_footer.php';
        exit();
     }

    /* Validate data */
     foreach($_POST as $field => $value)               
     {
        if(!empty($value))                             
        {
           if(eregi("name",$field) and
              !eregi("login",$field))
           {
              if (!ereg("^[A-Za-z' -]{1,50}$",$value))
              {
                 $errors[]="$value is not a valid name.";
              }
           }
           if(eregi("street",$field) or
             eregi("addr",$field) or eregi("city",$field))
           {
              if(!ereg("^[A-Za-z0-9.,' -]{1,50}$",$value))
              {
                 $errors[] = "$value is not a valid
                               address or city.";
              }
           }
           if(eregi("County",$field))
           {
              if(!ereg("[A-Za-z]{2}",$value))
              {
                $errors[]="$value is not a valid state.";
              }
           }
           if(eregi("email",$field))
           {
              if(!ereg("^.+@.+\\..+$",$value))
              {
                 $errors[] = "$value is not a valid
                              email address.";
              }
           }
           
		   
           if(eregi("phone",$field)
              or eregi("fax",$field))
           {
              if(!ereg("^[0-9)(xX -]{7,20}$",$value))
              {
                 $errors[] = "$value is not a valid
                              phone number. ";
              }
           }
        } // end if empty                             
     } // end foreach
     if(@is_array($errors))                           
     {
        $message_new = "";
        foreach($errors as $value)
        {
          $message_new .= $value." Please try
                                   again<br />";
        }
        extract($_POST);
        include("login_form.php");
		  include '_footer.php';
        exit();
     }

      /* clean data */
     //$cxn = mysqli_connect($host,$user,$passwd,$dbname);

     foreach($_POST as $field => $value)              
     {
        if($field != "Button" and $field != "do")
        {
           if($field == "password")
           {
              $password = strip_tags(trim($value));
           }
           else
		  
           {
              $fields[]=$field;
              $value = strip_tags(trim($value));
              $values[] =  
			 
			 
               mysqli_real_escape_string($cxn, $value);
              $$field = $value;
           }
        }
     }

      /* check whether user name already exists */
	  
     $sql = "SELECT LoginName FROM user
                    WHERE LoginName = '$LoginName'";  
     $result = mysqli_query($cxn,$sql)
               or die("Couldn't execute select query.");
     $num = mysqli_num_rows($result);                 
     if ($num > 0)                                    
     {
        $message_new = "$loginName already used.
                         Select another User Name.";
        include("login_form.php");
		  include '_footer.php';
        exit();
     }
    /* Add new member to database */
     else                                             
     {
        $today = date("Y-m-d");
        $fields_str = implode(",",$fields);
        $values_str = '"'.implode('","',$values);
        $fields_str .=",createDate";
        $values_str .='"'.",".'"'.$today;
        $fields_str .=",password";
        $values_str .= '"'.","."md5"."('".$password."')";
       
		$sql = '
			INSERT INTO user
			('.$fields_str.')
			VALUES ('.$values_str.');
		';
		
		

       $result = mysqli_query($cxn,$sql)
           or die("Couldn't execute insert query 2.");
       

       
		 if($result != false)
		 {
			 $_SESSION['auth']="yes";                     
			 $_SESSION['logname'] = $LoginName;            
			 
			 header('Location: '.$base_url.'new_member.php');
		 }
      }
    break;                                            

    default:                                          
        include("login_form.php");
		
  }
  
  include '_footer.php';
?>

When I remove include file for top(which has my html codes in), it seems to work when I remove that include file and information is entered in the databse.


I wonder if anyone can help me on this I will appreciate it :)

thanks

Member Avatar for Zagga

Hi Imratzio,

This problem is often caused by extra "whitespace". It will probably be blank lines in top.php (maybe around line 62)

If you search for "headers already sent" you will find some better explainations.


Hope this helps.
Zagga

-> remove if your top.php contains session_start(); statement.
-> Check that there are not any spaces at the end of those included files.
-> try placing this at the top of your page: <? ob_start(); ?>
-> remove white space before or after the opening and closing PHP tags (<?php . . . ?>).

What's the code for top.php?

The error I think is because in your script you're trying to redirect to another page using

header("location: page.php");

after the top.php has already sent header information.

Try using a javascript redirect or an html meta refresh. (google).

Hey guys !!. Thanks for replying I solved the problem but not sure how I did it :S.

All I did i swapped code on my login page into my include and vice versa.

But if I get the error again I will reconsider your options :D


Thanks Guuuys :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.