473,395 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
11 6029
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
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
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
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
"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
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
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
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
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stijn Goris | last post by:
Hi all, I have a question regarding the header function. I send a browser to a certain page (eg first.php ) wich sends no output to the browser. This page sends the browser to another page (eg...
13
by: Dave Smithz | last post by:
Hi there, I have a php script that does some form input validation. As it verifies each field if the field has incorrect data, it appends an error message to an $error array. E.g. if...
5
by: Duderino82 | last post by:
I'm working on a very simple peace of php where basically there is a form and 3 buttoms. One refreshed the page, one posts the form, and another one (since this form contains values of a record)...
2
by: hagenaer | last post by:
Hello, I'm building a simple shopping cart to work with PayPal. I'd like to have the user post his basket to my page, validate the input, then redirect him _with his validated form data_ to...
4
by: andre rodier | last post by:
Hello, I need to display or propose a jpeg image on a web page. When I need to display the image, I use this code : header("Content-Length: $fileSize") ; header("Content-Type: $type") ;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.