Problem: Cannot modify header information ....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Problem: Cannot modify header information ....

Post 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.""));

?>
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

you should use header() before any -html tag
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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...
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

but there isn't any html-tag.
Or am i wrong?
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

<script>
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post 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.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 :)
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post 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."")); 

?>
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 ;)
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
Post Reply