Can't run headers with OB

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
romchik19
Forum Newbie
Posts: 3
Joined: Sat Oct 09, 2010 1:31 pm

Can't run headers with OB

Post 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
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Can't run headers with OB

Post 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.
romchik19
Forum Newbie
Posts: 3
Joined: Sat Oct 09, 2010 1:31 pm

Re: Can't run headers with OB

Post 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();
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can't run headers with OB

Post 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".
romchik19
Forum Newbie
Posts: 3
Joined: Sat Oct 09, 2010 1:31 pm

Re: Can't run headers with OB

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can't run headers with OB

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply