Results 1 to 2 of 2

Thread: "Cannot modify header information" Warning on PHP Login Page

  1. #1
    Join Date
    Aug 2009
    Location
    Texas
    Posts
    193

    "Cannot modify header information" Warning on PHP Login Page

    I have a page called index.php that contains a form. If the user signs in correctly, it creates a cookie and directs them to edit.php. On that page, I would like to check to see if the cookie has been set yet, and if not redirect them back to the login page.

    It works if the cookie is set. However, if the cookie is not set, instead of redirecting back to index.php, I get the following warning:

    "Warning: Cannot modify header information - headers already sent by (output started at /home/austini2/public_html/staff/edit/edit.php:2) in /home/austini2/public_html/staff/edit/edit.php on line 7"

    I'm pretty new to PHP, so I don't know what to do at this point.

    Here is a simplified version of both pages with comments:

    index.php:
    PHP Code:
    <?php
    $username 
    "myusername";
    $password "abcd123";
    $randomword "therandomword";

    // If cookie is set correctly, redirect to edit.php
    if ( isset($_COOKIE['loginPage']) ) {
        if ( 
    $_COOKIE['loginPage'] == md5($password.$randomword) ) {
            
    header ("Location: edit.php");
        exit;
        }
    }

    // If user has tried to login, proceed:
    if (isset($_GET['p']) && $_GET['p'] == "login") {
        
    // If username and password is correct, set cookie and redirect to edit.php
        
    if ($_POST['username'] == $username && $_POST['password'] == $password) {
            
    setcookie"loginPage"md5($_POST['password'].$randomword) );
            
    header("Location: edit.php");
        }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en_us" xml:lang="en-us">
    <head>
    // ...
    </head>
    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post">
      <input type="text" id="username" name="username" />
      <input type="password" id="password" name="password" />
      <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    edit.php:
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <?php
    $password 
    "abcd123";
    $randomword "therandomword";

    // If cookie is not set, redirect  to index.php
    if ( !isset($_COOKIE['loginPage']) ) {
        
    header ("Location: index.php");
        exit;
    }
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en_us" xml:lang="en-us">
    <head>
    // ...
    <body>
    // ...
    </body>
    </html>
    edit.php is located at http://www.austinisdfinearts.org/staff/edit/edit.php if you would like to take a look.

    Also, is it important whether the PHP goes before or after the doctype?

    Thanks in advance.
    Last edited by zachsformacs95; 01 Jan 2011 at 06:34 PM.

  2. #2
    Join Date
    Aug 2009
    Location
    Texas
    Posts
    193
    Never mind. I moved the PHP before the doctype in edit.php and it worked.
    Last edited by zachsformacs95; 01 Jan 2011 at 07:56 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •