tags:

views:

3446

answers:

10
include('header.php');

$name = $_POST['name'];
$score = $_POST['score'];
$dept = $_POST['dept'];

$MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':date', $date);
// Run the query
$MyDB->run();

header('Location:index.php');
    exit;

The above code keeps giving me an issue with the redirect. The error is:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15) in /Applications/MAMP/htdocs/testygubbins/OO/test/form.php on line 16.

I am totally flummoxed by this. Does anyone know what i should be doing to make it work? Cheers

EDIT

header.php code:

<?php
include('class.user.php');
include('class.Connection.php');

 $date = date('Y-m-j');

?>
<html>
<head>
 <link rel=StyleSheet href="css/style.css" type="text/css" media=screen>
 <title>Test</title>
</head>
<body>
<div id="page">
+1  A: 

Look at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php line 15.

At that position, it makes some output. Fix it. :)

myplacedk
line 15 is blank
Drew
no matter if it's blank -- a newline is output too.
gnud
A: 

try

header("Location: index.php")
lfx
Alas, still nothing
Drew
+1  A: 
m3rLinEz
Did take the html out as well. Thanks! :)
Drew
+7  A: 

Look carefully at your includes - perhaps you have a blank line after a closing ?> ?

This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.

Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.

(EDIT: looking at your header, you need to avoid doing any HTML output if you want to output headers, or use output buffering to capture it).

Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:

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:

Paul Dixon
Cheers dude, whitespace it was. Big fun!
Drew
It's not just the whitespace, it's the whole HTML output after the PHP block that's causing the error.
monzee
The edit of this post shows the point, the include has html code in it, that is send out immediatly to the client.
BeowulfOF
+1  A: 

You may have some "plain text" somewhere in php files that is interpreted as script output. It may be even a newline before or after the php script tag specifier (less-than + question mark + "php").

Besides, if I remember correctly, according to http specification, the "Location" header accepts only full URLs, not relative locations. Have that in mind too.

macbirdie
A: 

Don't include header.php. You should not output HTML when you are going to redirect.

Make a new file, eg. "pre.php". Put this in it:

<?php
include('class.user.php');
include('class.Connection.php');
?>

Then in header.php, include that, in stead of including the two other files. In form.php, include pre.php in stead of header.php.

myplacedk
+1  A: 

Alternatively, not to think about a newline or space somewhere in the file, you can buffer the output. Basically, you call ob_start() at the very beginning of the file and ob_end_flush() at the end. You can find more details at php.net ob-start function description.

Edit: If you use buffering, you can output HTML before and after header() function - buffering will then ignore the output and return only the redirection header.

ya23
A: 

Your include produces output, thereby making it impossible to send a http header later. Two option:

  1. Move the output somewhere after the include.
  2. Use output buffering, i.e. at the very start of your script, put ob_start(), and at the end, put ob_flush(). This enables PHP to first wait for all the output to be gathered, determine in what order to render it, and outputs it.

I would recommend you learn the second option, as it makes you far more flexible.

Martijn Heemels
A: 

Also see your php file text encoding. Mine was UTF-8 with BOM and it prevented the script to work. But now works flawlessly after removing the BOM...

julifos
A: 

I have a similar problem despite following all the advice above. The mailer script sends the email(s) but I get a 'headers already sent' error relating to Line 29 which is {header("Location: $thanksURL");} and no redirect to the confirmation page.

Here is the code:

$b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: The Librarian, Postal History Society"; $subject2 = "Thank you for contacting the Postal History Society"; $autoreply = "Thank you for your request. Somebody will get back to you as soon as possible, usually within 48 hours."; if($from == '') {print "You have not entered an email, please go back and try again";} else { if($name == '') {print "You have not entered a name, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) {header("Location: $thanksURL");} else {print "We encountered an error sending your mail, please notify [email protected]"; } } } ob_end_flush() ?>

Can anyone suggest what is wrong?

Mike

Mike