0

Possible Duplicate:
“Warning: Headers already sent” in PHP

I have a php page which when i fill out email filed and press enter it connect to the mail.php in this page after sending mail i want to go back to the page that i was but it gives me this error :

Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/users/teachers/mail.php:3) in /home/mysite/public_html/users/teachers/mail.php on line 15

this is the mail.php code :

<html>
<body>

<?php

$email = $_GET['email'] ;

$subject =$_GET['author'] ;

$message = $_GET['text'] ;

$to = "mail@mail.com";

$from = $email;

$headers = "From:" . $from;

mail($to,$subject,$message,$headers,$from);

?> 

    <script language="javascript">


alert('your mail has sent !');</script>

<?php

header('location:../teachers/index.php');

?>

</body>

</html>

what should i do ?

Community
  • 1
  • 1
Mohammad_Hosseini
  • 2,481
  • 3
  • 30
  • 53

3 Answers3

2

In HTTP, a response is split into two sections: headers and body. They are separated by a double line break.

By printing <html><body> at the top of your mail.php script, you have effectively told PHP you are done with headers and ready for output. As PHP is sending back the information to Apache, it has sent back the complete header set already (it needs to, because you have now started to send the actual response body).

You have two options:

  • Enable output buffering in your PHP installation (PHP will then buffer the response body until the end of the script's execution or until you explicitly call one of the ob*end() methods.
  • Change your page to send the email and then redirect before printing any output to the browser.
Colin M
  • 13,010
  • 3
  • 38
  • 58
  • do you have any other suggestion for this code ? could you give me an example code ? for the record in another page this code works fine but in this page it is not working . – Mohammad_Hosseini Dec 07 '12 at 21:55
1

Stop sending anything on the page where redirection is expected.

your code should look like this

<?php

$email = $_GET['email'] ;

$subject =$_GET['author'] ;

$message = $_GET['text'] ;

$to = "mail@mail.com";

$from = $email;

$headers = "From:" . $from;

mail($to,$subject,$message,$headers,$from);

header('location:../teachers/index.php');

?>
Raab
  • 34,778
  • 4
  • 50
  • 65
  • actually i have tried the code on the above i mean rabs code but i showed me the same error .. function redirect($page) { $s = ""; return $s; } – Mohammad_Hosseini Dec 08 '12 at 06:37
  • but with this code it fixed and worked : function redirect($page) { $s = ""; return $s; } print( redirect('../teachers/index.php')); – Mohammad_Hosseini Dec 08 '12 at 06:41
0

You have something in your code which writes something to the output buffer. The redirect has to be done before any output is written to the output buffer.

This won't work:

<?php
echo 'bla';
header('Location: index.php');

This will work:

<?php
header('Location: index.php');
Benjamin Paap
  • 2,744
  • 2
  • 21
  • 33