0

I'm getting the typical 'Warning: Cannot modify header information - headers already sent by (output started a... line 14)'. This is usually due to an echo statement before the header() method, but confusingly I don't have any echos being called before the header() itself. Please can you tell me where the output is occuring and why as its currently baffling me, and further, so I can fix this in the future.

Line 14 is a line within the content div:

<p>Lorem ipsum...</p>

Thanks in advanced,

Max.

<html>
<head>
<title></title>
<!-- CSS -->
<link ... />
</head>
<body class="index not-admin">
    <div id="wrapper">
    <div id="logo"></div>
        <div id="headerInvoice"> 
        </div>
<div id="content">
    <form name="signup" action="" method="GET">
        <input name="email" type="email" placeholder="Email Address" />
        <input type="submit" title="Submit!"/>
    </form>
    <?php
    if($_SERVER['REQUEST_URI'] == '/betas/') {
        header('Location:http://betas.maxk.me');
    }
    if (isset($_GET['email'])) {
        $email = $_GET['email'];
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error = 'Email Address is invalid.';
        }
        else {
            mysql_connect('localhost', '...', '...');
            mysql_select_db('...');
            $email = mysql_real_escape_string($email);
            if (mysql_num_rows(mysql_query("SELECT * FROM testers WHERE `email_address` = '$email'")) < 1) {
                mysql_query("INSERT INTO `testers` (`email_address`,`accepted`) VALUES ('$email','0')");
                $error = !mysql_error()? 'Success!' : "We came across an error, please try again! " . mysql_error();
            }
            else {
                $error = 'Sorry, but you have already signed up! Please try emailing me to see if I need any testers through my <a href="http://maxk.me">homepage</a>.';
            }
        }
        echo $error;
    } 
?>
<br />
<a href="#">Login</a>
</div>      
<div id="footer">
</div>
</div>
</body>
</html>
max_
  • 24,076
  • 39
  • 122
  • 211

6 Answers6

1

The problem is here:

if($_SERVER['request_URI'] == '/betas/') {
  header('Location:http://betas.maxk.me');
}

Calling header after any html in a file will cause an error. The PHP documentation explains this in depth.

Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
0

This doesn't just apply to echo specifically. Anything outside of <?php tags is also emitted and will cause this problem.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

You should put your redirection code at the top of your file, not embedded inside, as HTML has already been outputted at that point.

E.g.

<?php
    if($_SERVER['REQUEST_URI'] == '/betas/') {
        header('Location:http://betas.maxk.me');
    }
?>
<html>
...
JRL
  • 76,767
  • 18
  • 98
  • 146
0

Everything before this line:

<?php
  if($_SERVER['REQUEST_URI'] == '/betas/') {
    header('Location:http://betas.maxk.me');

  // .. etc

Count as output. By the time you've called header(), a bunch of content has already been sent to output. You could perhaps capture it with the ob_* functions, but I bet there's a better solution.

rjz
  • 16,182
  • 3
  • 36
  • 35
0

All of the HTML before the opening PHP tag counts as data being sent, so you need the PHP header statement right at the top of your file even above your HTML code.

OdinX
  • 4,135
  • 1
  • 24
  • 33
0

Anything that is not within <?php ?> tags is treated in the same way as an echo statement and is sent straight to the browser as it occurs in the script. Any header call (or anything that involves HTTP headers) must be from the first PHP block. Note that white space at the beginning of the file will cause this problem too.

Will Vousden
  • 32,488
  • 9
  • 84
  • 95