0

I have the following error:

Warning: Cannot modify header information - headers already sent by (output started at /home/sn2010/public_html/pagetwo.php:10) in /home/sn2010/public_html/pagetwo.php on line 15

It happens when I go to pagetwo.php... You can try it here: http://sn2010.x10.mx/

I'm not sure exactly why it's giving me that problem, but here's my codes on both pages:

index.html

  <form name="form1" action="pagetwo.php" method="POST">
    <input type="submit" name="bootoon" value="index">
  </form>

  <form name="form2" action="pagetwo.php" method="POST">
    <input type="submit" name="bootoon" value="http://www.facebook.com">
  </form>

  <form name="form3" action="pagetwo.php" method="POST">
    <input type="submit" name="bootoon" value="http://www.yoyogames.com">
  <form/>

pagetwo.php

<?php
  echo "<h2>SN2010<br /></h2>";
  echo "<hr align='left' width='680px' /><br /><br />";

  $redirect = "Location: " . $_REQUEST["bootoon"] . ".php";
  echo header($redirect);
?>

Anyone know why the webpages are creating this error? I've tried using ob_start(); and ob_end_flush();. So those two codes are out of the picture...

BretHudson
  • 134
  • 1
  • 8
  • 1
    Beginner rules: omit `?>` closing tags and use `ob_start()`. Also this particular PHP error message is easy to google. – mario Nov 22 '10 at 03:23
  • @mario, I've already Google'd it, and I didn't find anything that helped. ): – BretHudson Nov 22 '10 at 03:43
  • http://stackoverflow.com/search?q=%5Bphp%5D+Warning%3A+Cannot+modify+header+information Stackoverflow is polluted with question about it too. And that error message tells you **precisely** where the error-provoking output started. – mario Nov 22 '10 at 04:16
  • Interesting that this is a duplicate of a newer question. – faintsignal Jan 07 '17 at 01:41

3 Answers3

4

You shouldn't be echo()'ing anything before sending a redirect.

<?php   
  $redirect = "Location: " . $_REQUEST["bootoon"] . ".php";
  header($redirect);
  exit();
?>
Michael Low
  • 24,276
  • 16
  • 82
  • 119
  • Gives me the same error. – BretHudson Nov 22 '10 at 03:41
  • @bretboy129 As the other answers say, you can't echo anything onto your page before the call to header(). The error message you're getting is because there is something being outputted before that, if it wasn't those two lines then somewhere before that. Even just having a space or new line before your opening – Michael Low Nov 22 '10 at 03:50
3

You don't echo a header. And you don't echo anything BEFORE the header.

bcosca
  • 17,371
  • 5
  • 40
  • 51
  • I did not know that. I'm actually copying this straight from a book, so that's very strange... – BretHudson Nov 22 '10 at 03:41
  • The online PHP manual is the most authoritative source with lots of examples on usage. Throw away that book. – bcosca Nov 22 '10 at 03:48
  • I just might. I've found quite a few errors in here (and I'm just a beginner) in the first few chapters, but it's the only book that I can find on it. The online PHP manual confuses me... I'll just have to try to Google search and ask on here if I come across any problems. – BretHudson Nov 22 '10 at 04:27
3

You cannot echo any information onto the page prior to calling header and you do not need to echo the headercall.

Taken from PHP documentation for header, which I suggest you should be familiar with..

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.

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115