-1

This is my code... I cannot figure out why it keeps saying

Warning: Cannot modify header information - headers already sent by (output started at /opt/apache/htdocs/save/header.php:10) in /opt/apache/htdocs/****/attendance.php on line 41

Warning: Cannot modify header information - headers already sent by (output started at /opt/apache/htdocs/save/header.php:10) in /opt/apache/htdocs/****/attendance.php on line 42

I just want the login page work. I also want to change the $_SERVER[PHP_SELF()] to the actual url so that my css and such still work.

include('header.php');
$server = "serverName";
$dUsername = "username";
$dPass = "password";
$username = "username2";
$password = "password2";
$randomword = "randomword";

if(isset($_COOKIE['MyLoginPage'])) {
    if($_COOKIE['MyLoginPage'] == md5($password.$randomword)) {
        $conn = mysql_connect($server, $dUsername, $dPass) or die("error connecting to MySQL database");
        mysql_select_db("w3_save", $conn);
        $query = "SELECT DISTINCT MemberName FROM attendance;";
        $result = mysql_query($query, $conn) or die(mysql_error());
        echo "<div id='rightColumn'><div id='title'><h1>Attendance</h1></div><div id='content'>";
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            echo "Name: $row[MemberName] <br />";
        }
        echo "</div></div></body></html>";

        mysql_close($conn);
        exit;
    }
    else {
        echo "<p>Bad Cookie. Please clear them out and try again</p>";
        exit;
    }
}

if(isset($_GET['p']) && $_GET['p'] == 'login') {
    if($_POST['name'] != $username) {
        echo "<p>Sorry the username you entered is incorrect.</p>";
        exit;
    }
    else if($_POST['pass'] != $password) {
        echo "<p>Sorry the password you entered is incorrect.</p>";
        exit;
    }
    else if($_POST['name'] == $username && $_POST['pass'] == $password) {
        **setcookie('MyLoginPage', md5($_POST['pass'].$randomword));  LINE 41
        header("Location: $_SERVER[PHP_SELF]");** LINE 42
    } else {
        echo "<p>Sorry you could not be logged in at this time please try again</p>";
    }
}


<div id='rightColumn'>
            <div id='title'>
                <h1>Attendance</h1>
            </div>
            <div id='content'>
                <form action="``<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method='post'>
                <label>Username: <input type='text' name='name' id='name' /></label><br />
                <label>Password:  <input type='password' name='pass' id='pass' /></label><br />
                <input type='submit' id='submit' value='Login' />
                </form>
            </div>
        </div>
    </body>
</html>
Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
eric
  • 454
  • 2
  • 9
  • 21
  • 7
    See the long list of "Related" questions on the right? Pick one. – animuson Nov 02 '11 at 23:12
  • Take a look at attendance.php: Line 41, the error-message should be self-explaining. – Dr.Molle Nov 02 '11 at 23:15
  • yeah I've checked for echo statements before it, I've tried shifting around my code, I've tried some random other things and have not figured out my issue. I know there are related issues but I wanted to know more specifically to my code. Also I wanted some help with changing the $_SERVER['PHP_SELF'] to a url I would want. I'm not sure if that is just as easy as changing the statement to whatever I want. – eric Nov 02 '11 at 23:15
  • And next time, please read what appears just below the question title when you type it in and move on to the description in the "Ask Question" form. – Jon Nov 02 '11 at 23:16
  • Sorry, but what is so difficult to understand? You CANNOT outut ANYTHING before sending headers! (that means, using header(), setcookie(), and other special functions) – Mārtiņš Briedis Nov 02 '11 at 23:16
  • Check to make sure there are no spaces or whitespace before ` – nickb Nov 02 '11 at 23:17
  • I understand it is changing the header but it was perfectly fine doing that for a while and then randomly stopped wanting to do that. This is exactly the same code as the website I got it from. – eric Nov 02 '11 at 23:17
  • Also, double check line 10 in your header.php - it is outputting something, hence the error. – nickb Nov 02 '11 at 23:19

2 Answers2

3

As noted in the comments, the error message is pretty self explanatory. Clearly header.php is starting output. You should track it down and figure out why. A workaround would be to use output buffering:

ob_start();
include('header.php');
...
ob_end_flush();
?>

This will prevent any output from being sent before you call the flush function; Documenation

As to $_SERVER['PHP_SELF']: You should not be editing the superglobals. Rather in this case, the information you are after is in a different index: $_SERVER['REQUEST_URI']

Shad
  • 15,134
  • 2
  • 22
  • 34
1

You can try changing the line 42 to something like:

header("Location: $_SERVER[PHP_SELF]",TRUE,302);

redirecting to a new page and use sendcookies before parsing html.

I advice to use ob_start and end though like:

ob_start();
//your html
$content = ob_get_contents();
ob_end_clean();

Find more info there http://php.net/manual/pt_BR/function.ob-start.php

jribeiro
  • 3,387
  • 8
  • 43
  • 70