By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,518 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

header("Location: ..."); Problem

P: n/a
I get the following message when trying to run my script:

"Warning: Cannot modify header information - headers already sent by (output
started at /var/www/html/newcustomer.php:23) in
/var/www/html/newcustomer.php on line 332"

....newcustomer.php:23 is where my script begins, at line 23.

before that line, only comments.

The script recives data from a form, checks if the customer exists, if so,
it updates the data otherwise inserts a new customer into the db. Also some
validation is done before writing to the db.

In advance, thanks for your time and advice.

FM>

And here's the flow of my script:

<?php
include 'db.inc'
include 'error.inc.

//init error string
$errorString = "";

//clean & trim POSTed values

... ... ...

//validate data
if (empty($formvars["source"]))
$errorString .=
"\n<br>You must suply the name of the source.";

//validate other data

... ... ...

//end of validation

//check if errors exist
if (!empty($errorString))
{
//errors do exist, show them & exit
?>
<html>
<body>
<br>Errors were found.
<br>
<?=$errorString?>
</body>
</html>

<?php
exit;
}

//if all data valid, open connection and load into the db
if (!(connection = @ .....))
die( ... );
if (!mysql_select_db( ... , ... ))
showerror();

//Are the data being updated? Then update the database
if (!empty($cust_id))
{
$query = "UPDATE customer SET " .
"source = \"" . $formvars["source"] . "\", " .
... ... ...
... ... ...
WHERE cust_id =$cust_id";
}
else
//Create query to insert the data

$query = INSERT INTO customer
... ... ...";

//Run the query on the customer table

... ... ...

//Is this an insert?
if (empty($cust_id))
//find the cust_id of the new customer
$cust_id = mysql_insert_id();

//Now redirect the customer to the receipt page
header ("Location: cust_receipt.php?cust_id=$cust_id");
?>
Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In article <2p************@uni-berlin.de>, Francisco Mendez wrote:
I get the following message when trying to run my script:

"Warning: Cannot modify header information - headers already sent by (output
started at /var/www/html/newcustomer.php:23) in
/var/www/html/newcustomer.php on line 332"
//check if errors exist
if (!empty($errorString))
{
//errors do exist, show them & exit
?>
<html>
<body>
<br>Errors were found.
<br>
<?=$errorString?>
</body>
</html>

}

header ("Location: cust_receipt.php?cust_id=$cust_id");

http://be2.php.net/manual/en/function.header.php

Parts that may interest you:

Remember that header() must be called before any actual output is sent,
either by normal HTML tags, blank lines in a file, or from PHP. It is a
very common error to read code with include(), or require(), functions,
or another file access function, and have spaces or empty lines that
are output before header() is called. The same problem exists when
using a single PHP/HTML file.
Note: HTTP/1.1 requires an absolute URI as argument to Location:
including the scheme, hostname and absolute path, but some clients
accept relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself:

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #2

P: n/a
Tim Van Wassenhove wrote:
In article <2p************@uni-berlin.de>, Francisco Mendez wrote: http://be2.php.net/manual/en/function.header.php


Hi Tim: Before I posted to the NG, I have RTFM, but i'm still not able to
solve my problem. I have sent a sample of code expecting someone were able
to help me to find a solution.

Anyway thx for your time.

FM>
Jul 17 '05 #3

P: n/a
Nel
"Francisco Mendez" <if****@prodigy.net.mx> wrote in message
news:2p************@uni-berlin.de...
Tim Van Wassenhove wrote:
In article <2p************@uni-berlin.de>, Francisco Mendez wrote:

http://be2.php.net/manual/en/function.header.php


Hi Tim: Before I posted to the NG, I have RTFM, but i'm still not able to
solve my problem. I have sent a sample of code expecting someone were able
to help me to find a solution.

Anyway thx for your time.

FM>

FM, you may wish to read Tim's message again. He has indeed re-listed the
errant code above his reply and then explained why it's a problem.

Apologies for repeating Tim's message, but in clear terms - you cannot
output anything before a header command.

not a single space, not an echo, not "<HTML> blah blah </HTML>".

It's that simple. If you must display some html (briefly!) before
redirecting then you should use the refresh meta tag.
http://webdesign.about.com/cs/metatags/a/aa080300a.htm

Nel.
Jul 17 '05 #4

P: n/a
I noticed that Message-ID: <2p************@uni-berlin.de> from Francisco
Mendez contained the following:
Hi Tim: Before I posted to the NG, I have RTFM, but i'm still not able to
solve my problem. I have sent a sample of code expecting someone were able
to help me to find a solution.


You can't have ANY HTML output before the header call. It could be as
simple as a blank line at the top. Or HTML comments.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5

P: n/a
"Francisco Mendez" <if****@prodigy.net.mx> wrote in message
news:2p************@uni-berlin.de...
Tim Van Wassenhove wrote:
In article <2p************@uni-berlin.de>, Francisco Mendez wrote:

http://be2.php.net/manual/en/function.header.php


Hi Tim: Before I posted to the NG, I have RTFM, but i'm still not able to
solve my problem. I have sent a sample of code expecting someone were able
to help me to find a solution.

Anyway thx for your time.


You also stated that you had "comments" prior to line 23 but that your
script didn't start until line 23. What was above line 23?

- Virgil
Jul 17 '05 #6

P: n/a
Virgil Green wrote:


You also stated that you had "comments" prior to line 23 but that your
script didn't start until line 23. What was above line 23?

- Virgil


/* begin comments line #1
.....
....
*/ line #22
<?php
....

Just that only comments from line to line 22, script begins in line 23 with
the usual <?php
FM>
Jul 17 '05 #7

P: n/a
Geoff Berrow wrote:
I noticed that Message-ID: <2p************@uni-berlin.de> from Francisco
Mendez contained the following:
Hi Tim: Before I posted to the NG, I have RTFM, but i'm still not able to
solve my problem. I have sent a sample of code expecting someone were able
to help me to find a solution.


You can't have ANY HTML output before the header call. It could be as
simple as a blank line at the top. Or HTML comments.


Geoff:

I have from line 1 to line 22 only cpmments eclosed within php markers, i.e.

<?php line #1
/*
....
....
*/
?> line 22
<?php
//Script begins here

Jul 17 '05 #8

P: n/a
I noticed that Message-ID: <2p************@uni-berlin.de> from Francisco
Mendez contained the following:
?> line 22
<?php


And what do you think is between line 22 and line 23? :-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #9

P: n/a
"Francisco Mendez" <if****@prodigy.net.mx> wrote in message
news:2p************@uni-berlin.de...
Geoff Berrow wrote:
I noticed that Message-ID: <2p************@uni-berlin.de> from Francisco
Mendez contained the following:
Hi Tim: Before I posted to the NG, I have RTFM, but i'm still not able tosolve my problem. I have sent a sample of code expecting someone were ableto help me to find a solution.
You can't have ANY HTML output before the header call. It could be as
simple as a blank line at the top. Or HTML comments.


Geoff:

I have from line 1 to line 22 only cpmments eclosed within php markers,

i.e.
<?php line #1
/*
...
...
*/
?> line 22
<?php
file://Script begins here


Remove the end and beginning tags on 22 and 23. You have a carriage return
and/or line feed between them and that counts as output, I believe.

- Virgil

Jul 17 '05 #10

P: n/a

"Francisco Mendez" <if****@prodigy.net.mx> ???????/???????? ? ????????
?????????: news:2p************@uni-berlin.de...
Tim Van Wassenhove wrote:
In article <2p************@uni-berlin.de>, Francisco Mendez wrote:

http://be2.php.net/manual/en/function.header.php


Hi Tim: Before I posted to the NG, I have RTFM, but i'm still not able to
solve my problem. I have sent a sample of code expecting someone were able
to help me to find a solution.

Anyway thx for your time.

FM>

Try putting <img src="javascript:location='http://some.domain.com'"> in the
right place. It could help you with redirecting after output is sent


Jul 17 '05 #11

P: n/a
Francisco Mendez <if****@prodigy.net.mx> wrote in message news:<2p************@uni-berlin.de>...
Virgil Green wrote:


You also stated that you had "comments" prior to line 23 but that your
script didn't start until line 23. What was above line 23?

- Virgil


/* begin comments line #1
....
...
*/ line #22
<?php
...

Just that only comments from line to line 22, script begins in line 23 with
the usual <?php
FM>


Why would you put comments in a separate block? Combine them into a
single block, it's more efficient in PHP and it also prevents \n from
being sent to the HTTP headers, which is exactly what is happening and
causing your header() function to fail and throw warnings.

Phil
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.