Page 1 of 1

Can't run headers with OB

Posted: Sat Oct 09, 2010 1:37 pm
by romchik19
Hello , I'm new here.
I get into problem with headers , here is my code :

Code: Select all

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php 
$selected_answer=$_POST['choise'];
$poll_id=$_GET['poll_id'];
$choise_count="count".$selected_answer;
$backtopage=$_GET['backtopage'];


$query= "SELECT * FROM poll WHERE id={$poll_id}";
$result=mysql_query($query);
confirm_query($result);
$row=mysql_fetch_array($result);
$count_number=$row[$choise_count];
$count_number=$count_number+1;
$query="UPDATE poll SET {$choise_count}={$count_number} WHERE id={$poll_id}";
$result=mysql_query($query);
if($result){
	$cookieName="sakranelPoll".$poll_id;
	setcookie($cookieName, 1 , time()+(60*60*60));
	if($backtopage)
		$loc="refresh:5;url=index.php?page=".$backtopage;
	header($loc);
	echo "<br/><br/>הצבעתך התקבלה !<br/> תוך מספר שניות תועבר חזרה לעמוד";
}
?>
<?php include("includes/footer.php"); ?>
inside header I have :

Code: Select all

<?php ob_start(); ?>
inside footer I have :

Code: Select all

<?php ob_end_flush(); ?>
I get this warning :

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/content/39/6744039/html/sakranel/verify_poll.php:1) in /home/content/39/6744039/html/sakranel/verify_poll.php on line 21

Warning: Cannot modify header information - headers already sent by (output started at /home/content/39/6744039/html/sakranel/verify_poll.php:1) in /home/content/39/6744039/html/sakranel/verify_poll.php on line 24

Why does the OB does not work ?
Thank you verry much

Re: Can't run headers with OB

Posted: Sat Oct 09, 2010 5:30 pm
by cpetercarter
The error message is telling you that the first line of your script is sending something to the browser. Check that you do not have any blank spaces before or after the end of the code on each line.
Incidentally, why do you open and close php on each of the first four lines of your script? There is no need to do this. All you need is <?php at the start of your script and ?> at the end. Indeed, I suspect a blank space character after one of the closing tags is the cause of your problem.

Re: Can't run headers with OB

Posted: Sun Oct 10, 2010 5:47 am
by romchik19
cpetercarter wrote:The error message is telling you that the first line of your script is sending something to the browser. Check that you do not have any blank spaces before or after the end of the code on each line.
Incidentally, why do you open and close php on each of the first four lines of your script? There is no need to do this. All you need is <?php at the start of your script and ?> at the end. Indeed, I suspect a blank space character after one of the closing tags is the cause of your problem.
I really can't find the problem ! I have tried to make a simple page with headers and ob_start(); ob_end_flush(); and it works ,
but this one , I can't get the catch why it does not works.
I get rid of all includes and put all code in one document , It still showing me the same error !
I did ctrl+a on all the text from notepad++ , I have checked spaces and didn't find anything special.

Code: Select all

<?php
ob_start();
// Database Constants
define("DB_SERVER", "XXX");
define("DB_USER", "XXX");
define("DB_PASS", "XXX");
define("DB_NAME", "XXX");
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
	die("Database connection failed: " . mysql_error());
}
mysql_set_charset('utf8',$connection);
// 2. Select a database to use
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
	die("Database selection failed: " . mysql_error());
}
$selected_answer=$_POST['choise'];
$poll_id=$_GET['poll_id'];
$choise_count="count".$selected_answer;
$backtopage=$_GET['backtopage'];
$query= "SELECT * FROM poll WHERE id={$poll_id}";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$count_number=$row[$choise_count];
$count_number=$count_number+1;
$query="UPDATE poll SET {$choise_count}={$count_number} WHERE id={$poll_id}";
$result=mysql_query($query);
if($result){
	$cookieName="sakranelPoll".$poll_id;
	setcookie($cookieName, 1 , time()+(60*60*60));
	if($backtopage)
		$location="refresh:5;url=index.php?page=".$backtopage;
		if ($location != NULL) {
			header("Location: {$location}");
			exit;
		}
	echo "<br/><br/>הצבעתך התקבלה !<br/> תוך מספר שניות תועבר חזרה לעמוד";
}
ob_end_flush();
?>

Re: Can't run headers with OB

Posted: Sun Oct 10, 2010 6:58 am
by requinix
Are you saving the file in UTF-8 encoding? PHP doesn't really support that.

Make sure you're using "ANSI" or "ASCII" or "ISO 8859-1".

Re: Can't run headers with OB

Posted: Sun Oct 10, 2010 8:21 am
by romchik19
don't know how it works now
i removed refresh 5 because safari says that microsoft windows dosn't support that.
and
added ob_clear(); at the end.
now it woks ok.
thanks for helps.
and I can't use ASCII because it's 3 languages site.

Re: Can't run headers with OB

Posted: Sun Oct 10, 2010 11:37 am
by s.dot

Code: Select all

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php 
Right there are 3 new lines that are being output. after the ?> and before the <?php

Code: Select all

<?php require_once("includes/connection.php");
require_once("includes/functions.php");
include("includes/header.php"); 
That, and fixing other errors where that is the problem, should solve it.