Please help. That darn header command – PHP

  php

Q(Question):

I’m hoping someone can help me with this. I’ve seen and tried various
solutions I’ve seen on the net, but nothing works.

Of course it works perfectly on localhost, but when I upload it to the
server (1and1.com) it gives errors. The user enters a code, the code is
verified, a new one is created and it’s supposed to send them to the next
page.
if (CodeExist($cCode) > 0){
$cNewCode = MakeCode();
$ok = InsertRec($cNewCode);

ob_start(); // doesn’t matter if I leave this in or take it out.
session_cache_limiter(‘public’); // suggestion from the internet. No
effect 🙁
session_start();
$_SESSION[‘newcode’] = $cNewCode;

print "session is set to:".$_SESSION[‘newcode’];
header("Location: info.php");
exit;

And here’s the output. Notice that the session was set even through the
error messages:

Warning: Cannot send session cookie – headers already sent by (output
started at /devel/joinnow.php:4) in /devel/joinnow.php on line 20

Warning: Cannot send session cache limiter – headers already sent (output
started at /devel/joinnow.php:4) in /devel/joinnow.php on line 20
session is set to:JSG598
Warning: Cannot add header information – headers already sent by (output
started at /devel/joinnow.php:4) in /devel/joinnow.php on line 25

Any help would be extremely helpful!
Thanks!!

A(Answer):

Update! I created two php test pages. Page 1 redirected to page 2. It
worked on my localhost AND on the internet server. So I’m scratching my
head thinking "What the hell?". THEN I tried it using my Dreamweaver
template. Holy crap, the simple one line redirection worked on my
localhost, but not on the internet server.

The Dreamweaver template is causing the mess!! Has any one else experienced
this and figured out how to fix it?

"Lochness" <so*****@somewhere.com> wrote in message
news:mOIcc.37239$Pk3.11329@pd7tw1no…

I’m hoping someone can help me with this. I’ve seen and tried various
solutions I’ve seen on the net, but nothing works.

Of course it works perfectly on localhost, but when I upload it to the
server (1and1.com) it gives errors. The user enters a code, the code is
verified, a new one is created and it’s supposed to send them to the next
page.
if (CodeExist($cCode) > 0){
$cNewCode = MakeCode();
$ok = InsertRec($cNewCode);

ob_start(); // doesn’t matter if I leave this in or take it out.
session_cache_limiter(‘public’); // suggestion from the internet. No
effect 🙁
session_start();
$_SESSION[‘newcode’] = $cNewCode;

print "session is set to:".$_SESSION[‘newcode’];
header("Location: info.php");
exit;

And here’s the output. Notice that the session was set even through the
error messages:

Warning: Cannot send session cookie – headers already sent by (output
started at /devel/joinnow.php:4) in /devel/joinnow.php on line 20

Warning: Cannot send session cache limiter – headers already sent (output
started at /devel/joinnow.php:4) in /devel/joinnow.php on line 20
session is set to:JSG598
Warning: Cannot add header information – headers already sent by (output
started at /devel/joinnow.php:4) in /devel/joinnow.php on line 25

Any help would be extremely helpful!
Thanks!!

A(Answer):

In article <xvLcc.40778$Ig.8376@pd7tw2no>,
"Lochness" <so*****@somewhere.com> wrote:

The Dreamweaver template is causing the mess!! Has any one else experienced
this and figured out how to fix it?

Probably empty lines or spaces somewhere. If you do a header("Location:
"), there is no output of any sort allowed before that. And that
includes white space before the <?php declaration.

JP


Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.

A(Answer):

"Jan Pieter Kunst" <de*****@cauce.org> schreef in bericht
news:de***************************@news1.news.xs4a ll.nl…

In article <xvLcc.40778$Ig.8376@pd7tw2no>,
"Lochness" <so*****@somewhere.com> wrote:

The Dreamweaver template is causing the mess!! Has any one else
experienced this and figured out how to fix it?

Probably empty lines or spaces somewhere. If you do a header("Location:
"), there is no output of any sort allowed before that. And that
includes white space before the <?php declaration.

JP


Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.

<snip> from previous post

if (CodeExist($cCode) > 0){
$cNewCode = MakeCode();
$ok = InsertRec($cNewCode);

ob_start(); // doesn’t matter if I leave this in or take it out.
session_cache_limiter(‘public’); // suggestion from the internet. No
effect 🙁
session_start();
$_SESSION[‘newcode’] = $cNewCode;

print "session is set to:".$_SESSION[‘newcode’];
header("Location: info.php");
exit;

</snip>

It is not probably it is definitely. Look at the print statement.
If you want to change the header information then this is the first thing
send back to the client. When sending white spaces, using echo’s or print
statements the header information is automatically send. The message:
"Warning: Cannot send session cache limiter – headers already sent" is
telling you did send something before using the header function. it is even
telling you where you did this (hence the line "/devel/joinnow.php on line
20").

I don’t know where you got your suggestions or other information from the
internet but www.php.net is a good place to start. Look at
http://www.php.net/manual/nl/function.header.php first 😉
HTH rob

A(Answer):

Lochness wrote:

Update! I created two php test pages. Page 1 redirected to page 2. It
worked on my localhost AND on the internet server. So I’m scratching my
head thinking "What the hell?". THEN I tried it using my Dreamweaver
template. Holy crap, the simple one line redirection worked on my
localhost, but not on the internet server.

The Dreamweaver template is causing the mess!! Has any one else
experienced this and figured out how to fix it?

Don’t use Dreamweaver? Seriously the only stuff I’d trust to use
‘auto-code’ for is flat, boring, static HTML. I’ve seen all sorts of
weirdness and client incompatibility from code generated in various IDE’s
(Like Dreamweaver). Unless you are stuck with it, maybe try hand-cranking
your code ‘ye olde fashioned’ way.

Not much help, I know, but thought I’d throw my $0.02 in.

Good luck,

James

Fortune cookie says:
Do unto others before they undo you.

A(Answer):

Thank you so much guys!!
After reading your replies I created a little test. With my template I
created a one statement page and at the top added:
——————–
<?php error_reporting(E_ALL);?>

<?php
header("Location: test2.htm");
?>
——————–
Notice the white space between the two php statements. I sent it up
to my server and it crashed. So I took out the whitespace, sent it up
to my server and it worked!! Then on my big program, I took out all
the blank lines and commented out the print statement. Tada! It
worked!! So thanks to you guys and grrrr to php and it’s tightass
attitude towards whitespace.

It worked on my localhost which is version 4.3.4 and the server is
4.2.3. Maybe the newer version isn’t as anal about whitespace.

Anyways. Thanks again!!

"Rob" <reply_@news_group.please> wrote in message news:<ef********************@amsnews03.chello.com> …

"Jan Pieter Kunst" <de*****@cauce.org> schreef in bericht
news:de***************************@news1.news.xs4a ll.nl…

In article <xvLcc.40778$Ig.8376@pd7tw2no>,
"Lochness" <so*****@somewhere.com> wrote:

The Dreamweaver template is causing the mess!! Has any one else experienced this and figured out how to fix it?

Probably empty lines or spaces somewhere. If you do a header("Location:
"), there is no output of any sort allowed before that. And that
includes white space before the <?php declaration.

JP


Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.

<snip> from previous post

if (CodeExist($cCode) > 0){
$cNewCode = MakeCode();
$ok = InsertRec($cNewCode);

ob_start(); // doesn’t matter if I leave this in or take it out.
session_cache_limiter(‘public’); // suggestion from the internet. No
effect 🙁
session_start();
$_SESSION[‘newcode’] = $cNewCode;

print "session is set to:".$_SESSION[‘newcode’];
header("Location: info.php");
exit;

</snip>

It is not probably it is definitely. Look at the print statement.
If you want to change the header information then this is the first thing
send back to the client. When sending white spaces, using echo’s or print
statements the header information is automatically send. The message:
"Warning: Cannot send session cache limiter – headers already sent" is
telling you did send something before using the header function. it is even
telling you where you did this (hence the line "/devel/joinnow.php on line
20").

I don’t know where you got your suggestions or other information from the
internet but www.php.net is a good place to start. Look at
http://www.php.net/manual/nl/function.header.php first 😉
HTH rob

A(Answer):

"Lochness" <s1******@hotmail.com> schrieb im Newsbeitrag
news:ac**************************@posting.google.c om…

Thank you so much guys!!
After reading your replies I created a little test. With my template I
created a one statement page and at the top added:
——————–
<?php error_reporting(E_ALL);?>

<?php
header("Location: test2.htm");
?>
——————–
Notice the white space between the two php statements. I sent it up
to my server and it crashed. So I took out the whitespace, sent it up
to my server and it worked!! Then on my big program, I took out all
the blank lines and commented out the print statement. Tada! It
worked!! So thanks to you guys and grrrr to php and it’s tightass
attitude towards whitespace.

It worked on my localhost which is version 4.3.4 and the server is
4.2.3. Maybe the newer version isn’t as anal about whitespace.

Anyways. Thanks again!!

I am not understanding everything about that stuff, but I am quite sure that
your problem is not about PHP but about HTTP. HTTP headers can’t be sent
after any output, regardless of what technique you use to send them. So if
you do this:

<?php
// PHP code
?>

<?php header("myheader"); ?>

you output white space via HTTP (and thus send the headers) in an area that
is not controlled by PHP.

If you do this:

<?
php echo "something";
header("myheader");
?>

your output is also sent via HTTP. I am quite sure that a clean arrangement
of the code in most cases solves that problem easily. For login or session
stuff I usually do it the way:

<?php
// handle session, check login, send cookie or whatever
$loggedin = "no"; // set variable with the result of the above actions
$message = "Wrong password, try it again!"; // set message according to the
above actions
?>
<html>
<head></head>
<body>
<?php
if($loggedin=="no") {
echo $message;
// display loginform
}
else {
// display contents
}
?>
</body>
</html>

Maybe your task is different, but you see the principle.

HTH
Markus

LEAVE A COMMENT