Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Headers have already been sent error

5 views
Skip to first unread message

jason...@gmail.com

unread,
Jul 8, 2006, 7:49:31 PM7/8/06
to
In my php code when I use the command:
header("Location: " . $_SERVER['HTTP_REFERER']);
I get an error saying the headers have already been sent.
What does this mean? I basically just want the page to refresh. Is
there a command in PHP that
makes the user's browser go to a specified URL?

Thanks,
Jason

Mironov Pavel

unread,
Jul 9, 2006, 7:12:04 AM7/9/06
to
Similar on that you try to send headers after last headers is sent.
You should send headers right at the beginning of a script

jason...@gmail.com писал(а):

jason...@gmail.com

unread,
Jul 9, 2006, 5:01:17 PM7/9/06
to
But what does this mean? I don't even think i call any header stuff in
other scripts. What are headers for?

- Jason

David Haynes

unread,
Jul 9, 2006, 5:10:06 PM7/9/06
to
You will get this message if your code emits any character (including a
space) before the header() is called.

Check your code for echo or print commands before the header().

-david-

Mironov Pavel

unread,
Jul 9, 2006, 7:00:40 PM7/9/06
to
> You will get this message if your code emits any character (including a
> space) before the header() is called.
>
> Check your code for echo or print commands before the header().
>
> -david-

And if you include any files before sending header, check that in the
end of these files there were no new lines or spaces after '?>'

jason...@gmail.com

unread,
Jul 9, 2006, 8:41:51 PM7/9/06
to
So all I really want to do is just to be able to refresh the current
page. Other than:
<META HTTP-EQUIV='Refresh' CONTENT='0;URL="blah.php'>
and

header("Location: " . $_SERVER['HTTP_REFERER']);

Are there other better ways of doing it?

- Jason

Mironov Pavel

unread,
Jul 9, 2006, 8:47:20 PM7/9/06
to

jason...@gmail.com wrote:

> So all I really want to do is just to be able to refresh the current
> page. Other than:
> <META HTTP-EQUIV='Refresh' CONTENT='0;URL="blah.php'>
> and
> header("Location: " . $_SERVER['HTTP_REFERER']);
>
> Are there other better ways of doing it?
>
> - Jason

Just one:
<script type="text/javascript">
window.location = document.referrer
</script>

David Haynes

unread,
Jul 9, 2006, 9:54:55 PM7/9/06
to
Some people operate with javascript disabled so it may not be
a good alternative.

-david-

babal

unread,
Jul 9, 2006, 11:50:18 PM7/9/06
to
Hi Jason

r u setting any cookie before the


> header("Location: " . $_SERVER['HTTP_REFERER']);

if so, that's the reason u r getting this error. check it out.

babal

jason...@gmail.com

unread,
Jul 10, 2006, 2:19:08 AM7/10/06
to
I do session_start() sometimes before that header, i think...

- Jason

Lestat

unread,
Jul 10, 2006, 8:08:23 PM7/10/06
to

Jason - you can do it like you are trying to. Like the other replies
before had said, just make sure you have NO output before that
header(); line. This includes any space(mentioned), and any html
(mentioned).

Good luck :)

cross * php dot net

unread,
Jul 11, 2006, 1:16:24 PM7/11/06
to
Like many of the previous post have stated, this is usually caused by
spaces, text, etc before a call to header() [or any other function that
would create/modify header content].
I have found that the easiest way to get around that specific error is
to use the output buffer functions to buffer the output of your code
untill all header modifying code is complete.

Although not a complete technical explanation, php basically starts
trying to stream created content to the client (through the httpd
server) and since the header officially stops with the first content
element, be it a space, an <html> or <DOCTYPE> tag, etc. If you buffer
the output, it will continue to be modifiable until the buffer is
released (or the end of page exicution, iirc)

resource: http://us3.php.net/outcontrol
example: (from previous url)
<?php

ob_start();

echo "Hello\n";

setcookie("cookiename", "cookiedata");

ob_end_flush();

?>

0 new messages