I need to call multiple headers and have managed to fix the error

'Warning: Cannot modify header information - headers already sent by (output started at '
all bar the last header by using ', false'

The line of code

Header('"Location:/".id_code(mysql_result($rs, 0, 'product_id'))', false);

and I keep getting the error message

Parse error: syntax error, unexpected T_STRING

any ideas.

    With the forums [ PHP ] tags you can see there's something wrong with the quoting:

    Header('"Location:/".id_code(mysql_result($rs, 0, 'product_id'))', false);
    

    I think what you want is:

    Header('Location: ' . id_code(mysql_result($rs, 0, 'product_id')), false);
    

      thank you I have just tired the following.

      Header('Location: ' . id_code(mysql_result($rs, 0, 'product_id')), false); 
      

      and it corrects the 'unexpected T_STRING' error but I am getting the 'headers already sent by (output started at ' error again

      with the previous ones I have the have the code with ' and then , false) at the end in order for it to work.

      is there a way to have double ' with getting the errors.

        That error is not due to any syntax problems, but because of code flow/sequencing. The error occurs because something earlier in your code has already generated output to the client. Once any sort of output is sent (including any text -- even a blank line -- not within <?php...?> tags), all pending HTTP headers are sent. Thereafter, you can no longer call any function that generates HTTP headers since they must be sent prior to any content.

          Thats why I used the

          , false

          function at the end

          I have three headers I need to call on the search, using it cancelled out the error with the second one, its just the third and last one

          The second header called

          Header('"Location:/".$manufacturer."/"', false);

          As you can see I used the false function and that resolved that error, I checked several times and ensured their is no whitespace either.

            I have three headers I need to call on the search, using it cancelled out the error with the second one, its just the third and last one

            What is the error with the second one? I think NogDog is right: you need to restructure your code. Perhaps you have indeed structured it such that no output is printed before your header() call, but it does not make sense to try and force multiple location headers. This is what you are trying to do by passing false as the second argument to header().

              There is not error with the second one now.

              And an input is printed the false function according to php.net

              The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type. For example:

              <?php
              header('WWW-Authenticate: Negotiate');
              header('WWW-Authenticate: NTLM', false);
              ?> 

              I just need to put

              "Location:/".id_code(mysql_result($rs, 0, 'product_id'))

              inside ' without causing a Parse error: syntax error, unexpected T_STRING, to enable me to call the , false function as menationed above.

                There is not error with the second one now.

                How did you fix it? If it was by adding that second argument to header(), then your "fix" is not a fix. You do not know what you are doing and are blindly guessing and checking.

                And an input is printed the false function according to php.net

                No, the PHP manual does not say anything about input being printed. What is does say is that providing false as the second argument to header() "you can force multiple headers of the same type". This is not useful in the context of location headers since the client can only end up in one location.

                inside ' without causing a Parse error: syntax error, unexpected T_STRING, to enable me to call the , false function as menationed above.

                False is not a function, and there is no parse error. The error, or rather, warning that you have is "Cannot modify header information" as you reported in your first post.

                  The error has always been with the third header(), I did not recieve an error before in IE the page would just not load, but in firefox it come out with "already sent by (output started at " relating the the thrid header()

                  I added ,false to the second header() and it loaded in IE but with same "already sent by (output started at '" as before in firefox.

                  As you can see in the code below

                  <?php
                  /* if a match on all search words failed to produce a result, we can try a match on any word by setting the flag below*/
                  $allow_wider_search = true;
                  /*if we're trying a wider search and it returns more results than the value below, we'll ignore the results as they will be too non-specific. */
                  $max_wider_search_results = 50;
                  /* limit the number of results returned by the query*/
                  $max_results = '100';
                  $sortby = 'price';
                  $sortreverse = false;
                  
                  if ($_GET["search"]!=null){
                      $term = $_GET["search"];
                  }
                  
                  if ($_POST["search"]!=null){
                      $term = $_POST["search"];
                  }
                  
                  if ($_GET["search2"]!=null){
                      $term = $_GET["search2"];
                  }
                  
                  if ($_POST["search2"]!=null){
                      $term = $_POST["search2"];
                  }
                  
                  $min = 0;
                  if ($_POST["min"]!=null){
                      $min = $_POST["min"];
                  }
                  
                  if ($_GET["min"]!=null){
                      $min = $_GET["min"];
                  }
                  
                  $max=100000;
                  if ($_POST["max"]!=null){
                      $max = $_POST["max"];
                  }
                  
                  if ($_GET["max"]!=null){
                      $max = $_GET["max"];
                  }
                  
                  $search_query = $term;
                  
                  
                  if (ereg("^search/.*$",$page_query))
                  {
                  	$results_exist=0;
                  
                  
                  if ($_GET["qterm"]!=null){
                  	$qterm = $_GET["qterm"];
                  	$followed = code_id($_GET["link"]);
                  	//record search selection, then follow link.
                  	$rs = mysql_query("select IFNULL((concat(replace(followedlist,'".$followed."',''),',".$followed."')), '".$followed."')
                  						from store_searchhistory where searchterm='".$qterm."'");
                  	if (mysql_num_rows($rs)){
                  		$followedlist = mysql_result($rs, 0, 0);
                  		if (substr($followedlist, 0, 1) == ",") $followedlist = substr($followedlist,1); //remove a csv comma if at start
                  		mysql_query("update store_searchhistory set followedlist='".$followedlist."' where searchterm='".$qterm."'");
                  		Header("Location:/".id_code($followed));
                  	}
                  }
                  
                  if (strlen($term) > 0){
                  	$dbterm="";
                  	$rs = mysql_query("select manufacturer as x from store_manufacturers where manufacturer = '".$term."' 
                  		union 
                  	select ifnull(
                  		(select (concat(ou.reference,'/', midl.reference, '/', inn.reference)) as x from store_categories as ou
                  		join store_categories as midl on ou.category_id = midl.category_parent 
                  		join store_categories as inn on midl.category_id = inn.category_parent 
                  		where inn.category like '".$term."%')
                  		,(select ifnull((select (concat(ou.reference,'/', inn.reference)) as x from store_categories as ou
                  		join store_categories as inn on ou.category_id = inn.category_parent 
                  		where inn.category like '".$term."%')
                  		,(select (reference) as x from store_categories 
                  		where category like '".$term."%')
                  	) as x))as x");
                  
                  	if (mysql_num_rows($rs)){
                  		$manufacturer = mysql_result($rs, 0, 0);
                  		$manufacturer = eregi_replace("[^0-9a-z_/]","_",$manufacturer);
                  		$manufacturer = eregi_replace("[_]+","_",$manufacturer);
                  		Header('"Location:/".$manufacturer."/"', false);
                  	}
                  
                  	$exact="";
                  	$any="";
                  	$term = str_replace('\"','"', $term);
                  	if(ereg('("[A-Za-z0-9/& ]+")', $term, $regs)){
                  		$exact = $regs[1];
                  		$term = str_replace($exact, '', $term);
                  	}
                  	$term = ereg_replace(' +',' ', $term); //remove extra spaces
                  	//
                  	$rs=mysql_query("select replace((select group_concat(searchterm) from(select searchterm from store_thesaurus as sa where uid in (
                  		select uid from store_thesaurus where link1 in( 
                  		select link1 from store_thesaurus where match searchterm against ('".$term."' in boolean mode)
                  		) )
                  		union select manufacturer as searchterm from store_manufacturers as sm 
                  		where match manufacturer against ('".$term."' in boolean mode)) as any),',',' ') as x");
                  	if (mysql_num_rows($rs)){
                  		$indiv_term = split(" ",$term);
                  		$any = mysql_result($rs,0,0);
                  		$any_term = split(" ",$any);
                  		$term2 = $term;
                  		if ($any != null){
                  			foreach ($indiv_term as $indiv){
                  				foreach ($any_term as $anystr){
                  				//remove found alternatives from search string
                  					$teststr = $indiv;
                  					if (strtolower($anystr) == strtolower($teststr)) $term2 = str_replace($teststr, '', $term2);
                  				}
                  			}
                  		}
                  		$all = ereg_replace('([A-Za-z0-9]+)', '+\\1', $term2); //add '+' before each search term that doesn't have a leading character (e.g. quote)
                  		$dbterm = $any." ".$all." ".$exact;
                  	}
                  	else {
                  		$dbterm = ereg_replace('([A-Za-z0-9]+)', '+\\1', $term)." ".$exact;
                  	}
                  
                  	echo $dbterm." : ".$any." : ".$all."<br>";
                  
                      $rs=runSearch($exact, $any, $term, $dbterm);
                  	if (mysql_num_rows($rs)) $results_exist=1;
                  	else{
                  		if ($allow_wider_search){
                  			$rs=runSearch($exact, $any, $term, $any." ".$term2." ".$exact);
                  			if (mysql_num_rows($rs) & (mysql_num_rows($rs) < $max_wider_search_results)) $results_exist=2;		
                  		}
                  	}
                  	echo $results_exist;
                  
                  	if (mysql_num_rows($rs)==1)
                  	{
                  		Header('Location: ' . id_code(mysql_result($rs, 0, 'product_id')), false); 
                  	}
                  
                  }
                  }
                  
                  function runSearch($exact, $any, $term, $dbterm) //$exact is quoted values, $any are non prefixed values, $term is original search term, $dbterm is parsed searchterm
                  {
                  	return mysql_query("
                  		select sp1.*, sm.manufacturer as m_name, scat.category as c_name from store_products as sp1
                  		left join
                  		store_manufacturers as sm
                  		on sm.manufacturer_id = sp1.manufacturer
                  		left join 
                  		store_categories as scat
                  		on scat.category_id = sp1.category
                  		where product_id in(
                  			select product_id from store_products where match (product_name) against (
                  				'".$dbterm."'
                  
                  			in boolean mode
                  
                  		)
                  		union
                  		select product_id from store_products where manufacturer_code like (CONCAT('".$term."', '%'))
                  		union
                  		select product_id from store_products where product_code like (CONCAT('".$term."', '%'))
                  	)and product_name like ('%') 
                  	and sm.manufacturer like(CONCAT((ifnull(
                  	(select manufacturer from store_manufacturers where match (manufacturer) AGAINST('".str_replace('"','',$exact)." ".$any."' in boolean mode) limit 1), '')
                  	),'%')
                  	)
                  	and scat.category like(CONCAT((IFNULL(
                  	(select category from store_categories where category LIKE '".str_replace('"','',$exact)."' limit 1), '')
                  	),'%')
                  	)
                  
                  	LIMIT 100 ;");
                  }
                  
                  function recordSearch($searchterm, $resultlist){
                  
                  	$rs = mysql_query("select (searchcount + 1) from store_searchhistory where searchterm='".$searchterm."' LIMIT 1");
                  	if (mysql_num_rows($rs)){
                  		mysql_query("update store_searchhistory set searchcount='".mysql_result($rs, 0, 0)."'
                  						, resultlist='".$resultlist."'
                  						, timestamp=now() where searchterm='".$searchterm."'");
                  	}
                  	else{
                  		mysql_query("insert into store_searchhistory (searchterm, searchcount, resultlist, timestamp)
                  				values('".$searchterm."',1, '".$resultlist."', now())");
                  	}
                  
                  }
                  
                  ?>
                  

                    Refer to the PHP manual on location headers and you will find that:
                    1. You should call exit (or die) immediately after sending a location header
                    2. You should use an absolute URI for the location header.

                    The problems that you are facing are mostly related to #1. Take for example this code snippet:

                    if (mysql_num_rows($rs)){
                        $manufacturer = mysql_result($rs, 0, 0);
                        $manufacturer = eregi_replace("[^0-9a-z_/]","_",$manufacturer);
                        $manufacturer = eregi_replace("[_]+","_",$manufacturer);
                        Header('"Location:/".$manufacturer."/"', false);
                    }
                    
                    $exact="";
                    $any="";
                    $term = str_replace('\"','"', $term);

                    The first problem is that the header's contents is probably wrong. You set it to the string '"Location:/".$manufacturer."/"', but you probably actually want it to be 'Location: /' . $manufacturer . '/'.

                    Now, assume that the location is correct, and suppose that mysql_num_rows($rs) returns a non-zero. Okay, so the location header is sent. Then $exact is set to ""... hold on, we just sent the location header. Why on earth would we want to assign stuff to $exact? The script should have redirected the user already!

                    Rather, it should be written as:

                    if (mysql_num_rows($rs)){
                        $manufacturer = mysql_result($rs, 0, 0);
                        $manufacturer = eregi_replace("[^0-9a-z_/]","_",$manufacturer);
                        $manufacturer = eregi_replace("[_]+","_",$manufacturer);
                        Header('Location: /' . $manufacturer . '/');
                        exit;
                    }
                    
                    $exact="";
                    $any="";
                    $term = str_replace('\"','"', $term);

                    Notice that I removed the false, i.e., no more second argument to header(). As I mentioned, this second argument is useless where location headers are concerned. But this time I added the exit; so the script will exit at that point and the user will be redirected as expected.

                      You have two echo statements (lines 126 and 136 of your code sample) before the last header() call (line 140). Therefore headers have already been sent by the first echo statement by the time you get to the final header() call, so it will fail with the "headers already sent" error.

                        Thank you for your help.

                        I have removed the previous header() and changed the code a bit but still on the final header() (the only one now) I get the error

                        'Warning: Cannot modify header information - headers already sent by (output started at'

                        which references the following on 133

                        echo $dbterm." : ".$any." : ".$all."<br>";

                        the new code

                        <?php
                        /*
                        Displaying the search page
                        Created: 13th September 2007
                        */
                        /* if a match on all search words failed to produce a result, we can try a match on any word by setting the flag below*/
                        $allow_wider_search = true;
                        /*if we're trying a wider search and it returns more results than the value below, we'll ignore the results as they will be too non-specific. */
                        $max_wider_search_results = 50;
                        /* limit the number of results returned by the query*/
                        $max_results = '100';
                        $sortby = 'price';
                        $sortreverse = false;
                        
                        if ($_GET["search"]!=null){
                            $term = $_GET["search"];
                        }
                        
                        if ($_POST["search"]!=null){
                            $term = $_POST["search"];
                        }
                        
                        if ($_GET["search2"]!=null){
                            $term = $_GET["search2"];
                        }
                        
                        if ($_POST["search2"]!=null){
                            $term = $_POST["search2"];
                        }
                        
                        $min = 0;
                        if ($_POST["min"]!=null){
                            $min = $_POST["min"];
                        }
                        
                        if ($_GET["min"]!=null){
                            $min = $_GET["min"];
                        }
                        
                        $max=100000;
                        if ($_POST["max"]!=null){
                            $max = $_POST["max"];
                        }
                        
                        if ($_GET["max"]!=null){
                            $max = $_GET["max"];
                        }
                        
                        $search_query = $term;
                        
                        
                        if (ereg("^search/.*$",$page_query))
                        {
                        	$results_exist=0;
                        
                        
                        if ($_GET["qterm"]!=null){
                        	$qterm = $_GET["qterm"];
                        	$followed = code_id($_GET["link"]);
                        	//record search selection, then follow link.
                        	$rs = mysql_query("select IFNULL((concat(replace(followedlist,'".$followed."',''),',".$followed."')), '".$followed."')
                        						from store_searchhistory where searchterm='".$qterm."'");
                        	if (mysql_num_rows($rs)){
                        		$followedlist = mysql_result($rs, 0, 0);
                        		if (substr($followedlist, 0, 1) == ",") $followedlist = substr($followedlist,1); //remove a csv comma if at start
                        		mysql_query("update store_searchhistory set followedlist='".$followedlist."' where searchterm='".$qterm."'");
                        		//header("Location:/".id_code($followed));
                        		//header("Location: /".id_code($followed));
                        		$str1 = id_code($followed);
                        	}
                        }
                        
                        if (strlen($term) > 0){
                        	$dbterm="";
                        	$rs = mysql_query("select manufacturer as x from store_manufacturers where manufacturer = '".$term."' 
                        		union 
                        	select ifnull(
                        		(select (concat(ou.reference,'/', midl.reference, '/', inn.reference)) as x from store_categories as ou
                        		join store_categories as midl on ou.category_id = midl.category_parent 
                        		join store_categories as inn on midl.category_id = inn.category_parent 
                        		where inn.category like '".$term."%')
                        		,(select ifnull((select (concat(ou.reference,'/', inn.reference)) as x from store_categories as ou
                        		join store_categories as inn on ou.category_id = inn.category_parent 
                        		where inn.category like '".$term."%')
                        		,(select (reference) as x from store_categories 
                        		where category like '".$term."%')
                        	) as x))as x");
                        
                        	if (mysql_num_rows($rs)){
                        		$manufacturer = mysql_result($rs, 0, 0);
                        		$manufacturer = eregi_replace("[^0-9a-z_/]","_",$manufacturer);
                        		$manufacturer = eregi_replace("[_]+","_",$manufacturer);
                        		$str2 = $manufacturer."/";
                        		//header('"Location:/".$manufacturer."/"', false);
                        	}
                        
                        	$exact="";
                        	$any="";
                        	$term = str_replace('\"','"', $term);
                        	if(ereg('("[A-Za-z0-9/& ]+")', $term, $regs)){
                        		$exact = $regs[1];
                        		$term = str_replace($exact, '', $term);
                        	}
                        	$term = ereg_replace(' +',' ', $term); //remove extra spaces
                        	//
                        	$rs=mysql_query("select replace((select group_concat(searchterm) from(select searchterm from store_thesaurus as sa where uid in (
                        		select uid from store_thesaurus where link1 in( 
                        		select link1 from store_thesaurus where match searchterm against ('".$term."' in boolean mode)
                        		) )
                        		union select manufacturer as searchterm from store_manufacturers as sm 
                        		where match manufacturer against ('".$term."' in boolean mode)) as any),',',' ') as x");
                        	if (mysql_num_rows($rs)){
                        		$indiv_term = split(" ",$term);
                        		$any = mysql_result($rs,0,0);
                        		$any_term = split(" ",$any);
                        		$term2 = $term;
                        		if ($any != null){
                        			foreach ($indiv_term as $indiv){
                        				foreach ($any_term as $anystr){
                        				//remove found alternatives from search string
                        					$teststr = $indiv;
                        					if (strtolower($anystr) == strtolower($teststr)) $term2 = str_replace($teststr, '', $term2);
                        				}
                        			}
                        		}
                        		$all = ereg_replace('([A-Za-z0-9]+)', '+\\1', $term2); //add '+' before each search term that doesn't have a leading character (e.g. quote)
                        		$dbterm = $any." ".$all." ".$exact;
                        	}
                        	else {
                        		$dbterm = ereg_replace('([A-Za-z0-9]+)', '+\\1', $term)." ".$exact;
                        	}
                        
                        	echo $dbterm." : ".$any." : ".$all."<br>";
                        
                        	$rs=runSearch($exact, $any, $term, $dbterm);
                        	if (mysql_num_rows($rs)) $results_exist=1;
                        	else{
                        		if ($allow_wider_search){
                        			$rs=runSearch($exact, $any, $term, $any." ".$term2." ".$exact);
                        			if (mysql_num_rows($rs) & (mysql_num_rows($rs) < $max_wider_search_results)) $results_exist=2;		
                        		}
                        	}
                        	echo $results_exist;
                        
                        	if (mysql_num_rows($rs)==1)
                        	{
                        		$str3 = id_code(mysql_result($rs, 0, 'product_id'));
                        		header("location: $str3", false);  
                        	}
                        
                        }
                        }
                        //header("Location: /".id_code($followed));
                        //header('"Location:/".$manufacturer."/"', false);
                        //Header("Location:/".id_code(mysql_result($rs, 0, 'product_id')));
                        //$str1 = id_code($followed);
                        //$str3
                        
                        function runSearch($exact, $any, $term, $dbterm) //$exact is quoted values, $any are non prefixed values, $term is original search term, $dbterm is parsed searchterm
                        {
                        	return mysql_query("
                        		select sp1.*, sm.manufacturer as m_name, scat.category as c_name from store_products as sp1
                        		left join
                        		store_manufacturers as sm
                        		on sm.manufacturer_id = sp1.manufacturer
                        		left join 
                        		store_categories as scat
                        		on scat.category_id = sp1.category
                        		where product_id in(
                        			select product_id from store_products where match (product_name) against (
                        				'".$dbterm."'
                        
                        			in boolean mode
                        
                        		)
                        		union
                        		select product_id from store_products where manufacturer_code like (CONCAT('".$term."', '%'))
                        		union
                        		select product_id from store_products where product_code like (CONCAT('".$term."', '%'))
                        	)and product_name like ('%') 
                        	and sm.manufacturer like(CONCAT((ifnull(
                        	(select manufacturer from store_manufacturers where match (manufacturer) AGAINST('".str_replace('"','',$exact)." ".$any."' in boolean mode) limit 1), '')
                        	),'%')
                        	)
                        	and scat.category like(CONCAT((IFNULL(
                        	(select category from store_categories where category LIKE '".str_replace('"','',$exact)."' limit 1), '')
                        	),'%')
                        	)
                        
                        	LIMIT 100 ;");
                        }
                        
                        function recordSearch($searchterm, $resultlist){
                        
                        	$rs = mysql_query("select (searchcount + 1) from store_searchhistory where searchterm='".$searchterm."' LIMIT 1");
                        	if (mysql_num_rows($rs)){
                        		mysql_query("update store_searchhistory set searchcount='".mysql_result($rs, 0, 0)."'
                        						, resultlist='".$resultlist."'
                        						, timestamp=now() where searchterm='".$searchterm."'");
                        	}
                        	else{
                        		mysql_query("insert into store_searchhistory (searchterm, searchcount, resultlist, timestamp)
                        				values('".$searchterm."',1, '".$resultlist."', now())");
                        	}
                        
                        }
                        
                        ?>
                        

                          Yes,
                          as told before in this thread
                          Any echo will need a header to be sent
                          So we know after any print or echo output has been done,
                          - Header has been sent!

                            So am I correct in thinking if I move the echos to below the header that will be correct now?

                              Just to clarify a bit: all headers are sent at the same time. header() commands in PHP essentially add/replace HTTP headers into the queue of headers to be sent -- they do not actually send send the headers. Once any command (echo, print, etc.) or text not within <?php...?> tags is encountered causing output to be generated, all headers in that queue are then sent before sending the text output. After that point, no more headers may be sent (since the headers, by definition, must precede the content); therefore you cannot use any more PHP commands which attempt to set HTTP headers (header(), session_start(), setcookie() for example).

                                JodPhp wrote:

                                So am I correct in thinking if I move the echos to below the header that will be correct now?

                                Yes.

                                  Write a Reply...