Page 1 of 1

Problem: Cannot modify header information ....

Posted: Fri Sep 17, 2004 6:06 am
by potato
Hi there again,

i have to redirect the visitor to a specific url, but i get following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/bevibed.be/httpdocs/e107/e107_handlers/vote.php:46) in /home/httpd/vhosts/bevibed.be/httpdocs/e107/e107_handlers/vote.php on line 57
I tried with ob_start & ob_end_flush, but nothing worked.
Can somebody help me?

My code looks like this:

Code: Select all

<?php

require_once('../../Connections/e107.php'); 

$userid_voter = $HTTP_POST_VARS['user_voter'];
$id_artdj = $HTTP_POST_VARS['id_artdj'];
$points = $HTTP_POST_VARS['points'];
$url = $HTTP_POST_VARS['url'];
$type = $HTTP_POST_VARS['type'];



//Indien type artiest is********************************************************
if ($type == "artist"){

mysql_select_db($database_e107, $e107);
$query_Recordset1 = sprintf("SELECT * FROM votes WHERE userid = '$userid_voter' AND type_id = '$id_artdj'");
$Recordset1 = mysql_query($query_Recordset1, $e107) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$query_Recordset2 = sprintf("SELECT * FROM bands WHERE band_id = $id_artdj");
$Recordset2 = mysql_query($query_Recordset2, $e107) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);

$totaal_voor = $row_Recordset2['vote_totaal'];
$aantal_voor = $row_Recordset2['vote_aantal'];


$totaal_na = $totaal_voor + $points;
$aantal_na = $aantal_voor + 1;
$gemiddelde = $totaal_na / $aantal_na;
$gemiddelde = (int)$gemiddelde;

if ($totalRows_Recordset1 == "0"){
  $insertSQL = sprintf("INSERT INTO votes (userid, vote_type, type_id) VALUES ('$userid_voter', '$type', '$id_artdj')");
  $Result1 = mysql_query($insertSQL, $e107) or die(mysql_error());
  
  $updateSQL = ("UPDATE bands SET vote_totaal ='$totaal_na', vote_aantal = '$aantal_na', vote_gemiddelde = '$gemiddelde' WHERE band_id='$id_artdj'");
  $Result2 = mysql_query($updateSQL, $e107) or die(mysql_error());?>
  
<script>alert("Thanx for your vote!")</script> <?
  
}else{?>

<script>alert("You already voted on this item!")</script> 

<? }}

//Indien type dj is********************************************************
if ($type == "dj"){}

 

//ga naar reffering url*****************************************************

header(sprintf("Location: ".$url.""));

?>

Posted: Fri Sep 17, 2004 6:13 am
by dethron
you should use header() before any -html tag

Posted: Fri Sep 17, 2004 6:20 am
by John Cartwright
dethron wrote:you should use header() before any -html tag
You HAVE to user header,session_start, cookie etc before any output...

Posted: Fri Sep 17, 2004 6:48 am
by potato
but there isn't any html-tag.
Or am i wrong?

Posted: Fri Sep 17, 2004 6:50 am
by dethron
<script>

Posted: Fri Sep 17, 2004 6:58 am
by potato
OK, if i put the header before the <script>-tag, the page don't load, i think the server makes a loop or something.
I've read about ob_start(), but where should i place the start en end_flush?

Sorry if i work you on the nerves :) , but i really have to get over this.

Posted: Fri Sep 17, 2004 7:01 am
by dethron
By the way, if you label your code as PHP instead CODE, we can see some nice colors,
it looks whole green now :)

Posted: Fri Sep 17, 2004 7:03 am
by potato
Sorry, didn't know that.

here it is:

Code: Select all

<?php 

require_once('../../Connections/e107.php'); 

$userid_voter = $HTTP_POST_VARS['user_voter']; 
$id_artdj = $HTTP_POST_VARS['id_artdj']; 
$points = $HTTP_POST_VARS['points']; 
$url = $HTTP_POST_VARS['url']; 
$type = $HTTP_POST_VARS['type']; 



//Indien type artiest is******************************************************** 
if ($type == "artist"){ 

mysql_select_db($database_e107, $e107); 
$query_Recordset1 = sprintf("SELECT * FROM votes WHERE userid = '$userid_voter' AND type_id = '$id_artdj'"); 
$Recordset1 = mysql_query($query_Recordset1, $e107) or die(mysql_error()); 
$row_Recordset1 = mysql_fetch_assoc($Recordset1); 
$totalRows_Recordset1 = mysql_num_rows($Recordset1); 

$query_Recordset2 = sprintf("SELECT * FROM bands WHERE band_id = $id_artdj"); 
$Recordset2 = mysql_query($query_Recordset2, $e107) or die(mysql_error()); 
$row_Recordset2 = mysql_fetch_assoc($Recordset2); 

$totaal_voor = $row_Recordset2['vote_totaal']; 
$aantal_voor = $row_Recordset2['vote_aantal']; 


$totaal_na = $totaal_voor + $points; 
$aantal_na = $aantal_voor + 1; 
$gemiddelde = $totaal_na / $aantal_na; 
$gemiddelde = (int)$gemiddelde; 

if ($totalRows_Recordset1 == "0"){ 
  $insertSQL = sprintf("INSERT INTO votes (userid, vote_type, type_id) VALUES ('$userid_voter', '$type', '$id_artdj')"); 
  $Result1 = mysql_query($insertSQL, $e107) or die(mysql_error()); 
  
  $updateSQL = ("UPDATE bands SET vote_totaal ='$totaal_na', vote_aantal = '$aantal_na', vote_gemiddelde = '$gemiddelde' WHERE band_id='$id_artdj'"); 
  $Result2 = mysql_query($updateSQL, $e107) or die(mysql_error());?> 
  
<script>alert("Thanx for your vote!")</script> <? 
  
}else{?> 

<script>alert("You already voted on this item!")</script> 

<? }} 

//Indien type dj is******************************************************** 
if ($type == "dj"){} 



//ga naar reffering url***************************************************** 

header(sprintf("Location: ".$url."")); 

?>

Posted: Fri Sep 17, 2004 7:08 am
by dethron
May be you can use document.location.replace,
but i dont like the code imagining so.

Why don't you open a pop-up window(from the main page) to display the results,
and to thanks ;)

Posted: Fri Sep 17, 2004 7:10 am
by dethron
And one more hint, if you use functions the code will be more robust and more everything.

You can write functions to access to db and retrieve elements.

If HTML and SQL are not at the same page, i think it is better :)

Posted: Fri Sep 17, 2004 9:34 am
by timvw
my guess is that the included file contains additional blank lines after the last line of php code like

Code: Select all

------ whatever connections.php
<?php
..
...
?>


------------ end of whatever connections.php
If you include this file, it will output 3 blank lines.