HEADERS Already sent (NewBie question)

This is a discussion on HEADERS Already sent (NewBie question) within the PHP Language forums, part of the PHP Programming Forums category; Hello again... I was wondering is there any way to check or output in the errors string which file or ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-13-2005
Angelos
 
Posts: n/a
Default HEADERS Already sent (NewBie question)

Hello again... I was wondering is there any way to check or output in the
errors string which file or where in my code the headers have been sent ?

I am trying to use this script
************************************************** *****************************************
function redirect($url) {
if(!headers_sent()) {
header('Location: http://'.$_SERVER['HTTP_HOST'] .
dirname($_SERVER 'PHP_SELF']) . '/' . $url);
}
else {
die('Could not redirect; Headers already sent.');
}
}
************************************************** *******************************************
But no luck in some redirect attempts. It works in some files but I can't
figure out when it doesn't work.

bellow is a part of the code that it doesn't work
************************************************** *************************
<? require_once 'http.php'; ?> <====HEre is the Redirection Function
<?php require_once('db_conn.php'); ?>
<? session_start(); ?><? header("Cache-control: private"); // IE 6 Fix. ?>
<?


if (isset($_REQUEST['contcat']) && isset($_REQUEST['action'])) {
switch ($_REQUEST['contcat']) {
case 'content':
switch ($_REQUEST['action']) {
case 'add_webpage':
printf (($_POST['title']) . ($_POST['Contentbody']) .
($_SESSION['user_id']));
if (isset($_POST['title']) and
isset($_POST['Contentbody']) and isset($_SESSION['user_id'])) {
mysql_select_db($db, $db);
$sql = "INSERT INTO bla bla bla";
mysql_query($sql, $db) or die('Could not
content'.mysql_error());
}
redirect('index.php'); <<<<<=================
break;
default:
break;
}
default:
break;
}
}
************************************************** *************************
I would really appreciate some help.
Thanks Angelos.


Reply With Quote
  #2 (permalink)  
Old 05-13-2005
Alistair Baillie SS2002
 
Posts: n/a
Default Re: HEADERS Already sent (NewBie question)

As the errors say, headers already sent.

This means that you have already tried to send text, or something to the
browser, either using an echo, or print statement.

Also check your code, to make sure you have NO whitespace outside the PHP
tags, otherwise this will cause apache to send the whitespace, therefore
removing your ability to set headers.

Headers, as the name suggests must be sent BEFORE you can send any data to
be displayed.

- Ali*


"Angelos" <angelos@redcatmedia.net> wrote in message
news:d61uaf$nfd$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
> Hello again... I was wondering is there any way to check or output in the
> errors string which file or where in my code the headers have been sent ?
>
> I am trying to use this script
> ************************************************** *****************************************
> function redirect($url) {
> if(!headers_sent()) {
> header('Location: http://'.$_SERVER['HTTP_HOST'] .
> dirname($_SERVER 'PHP_SELF']) . '/' . $url);
> }
> else {
> die('Could not redirect; Headers already sent.');
> }
> }
> ************************************************** *******************************************
> But no luck in some redirect attempts. It works in some files but I can't
> figure out when it doesn't work.
>
> bellow is a part of the code that it doesn't work
> ************************************************** *************************
> <? require_once 'http.php'; ?> <====HEre is the Redirection Function
> <?php require_once('db_conn.php'); ?>
> <? session_start(); ?><? header("Cache-control: private"); // IE 6 Fix.
> ?>
> <?
>
>
> if (isset($_REQUEST['contcat']) && isset($_REQUEST['action'])) {
> switch ($_REQUEST['contcat']) {
> case 'content':
> switch ($_REQUEST['action']) {
> case 'add_webpage':
> printf (($_POST['title']) . ($_POST['Contentbody']) .
> ($_SESSION['user_id']));
> if (isset($_POST['title']) and
> isset($_POST['Contentbody']) and isset($_SESSION['user_id'])) {
> mysql_select_db($db, $db);
> $sql = "INSERT INTO bla bla bla";
> mysql_query($sql, $db) or die('Could not
> content'.mysql_error());
> }
> redirect('index.php'); <<<<<=================
> break;
> default:
> break;
> }
> default:
> break;
> }
> }
> ************************************************** *************************
> I would really appreciate some help.
> Thanks Angelos.
>



Reply With Quote
  #3 (permalink)  
Old 05-13-2005
Alvaro G Vicario
 
Posts: n/a
Default Re: HEADERS Already sent (NewBie question)

*** Angelos wrote/escribió (Fri, 13 May 2005 10:08:18 +0000 (UTC)):
> Hello again... I was wondering is there any way to check or output in the
> errors string which file or where in my code the headers have been sent ?


This is the error message I get with PHP 4.1.2 when such error happens:

Warning: Cannot add header information - headers already sent by (output
started at /mnt/webs/Pruebas/www/borrame.php:7) in
/mnt/webs/Pruebas/www/borrame.php on line 9

Have you disabled error reporting? Try this:

error_reporting(E_ALL);


--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Reply With Quote
  #4 (permalink)  
Old 05-14-2005
Angelos
 
Posts: n/a
Default Re: HEADERS Already sent (NewBie question)

>> Hello again... I was wondering is there any way to check or output in the
>> errors string which file or where in my code the headers have been sent ?

>
> This is the error message I get with PHP 4.1.2 when such error happens:
>
> Warning: Cannot add header information - headers already sent by (output
> started at /mnt/webs/Pruebas/www/borrame.php:7) in
> /mnt/webs/Pruebas/www/borrame.php on line 9
>
> Have you disabled error reporting? Try this:
>
> error_reporting(E_ALL);


I have enabled the error reporting. And I also have
error_reporting = E_ALL
which should report my errors as you say above. Although it doesn't work for
some reason.

The only error that I get is the one that I wrote in my function:
function redirect($url)
{
if(!headers_sent())
{
header('Location: http://'.$_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']) . '/' . $url);
}
else
{
die('Could not redirect; Headers already sent.');
}
}


Reply With Quote
  #5 (permalink)  
Old 05-14-2005
Angelos
 
Posts: n/a
Default Re: HEADERS Already sent (NewBie question)

> printf (($_POST['title']) . ($_POST['Contentbody']) .
> ($_SESSION['user_id']));

Problem Solved... A stupid error that noone saw !!!
:) Thanks guys !


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:15 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0