Script not working as meant to?

Discussion in 'PHP' started by Matt Ridge, Oct 3, 2011.

  1. #1
    Ok, I have been fighting on another form to get people to actually understand what is going on... and I am asking people here now, since the people I was talking to didn't want their precious book to be slandered...

    www.kaboomlabs.com

    that site is my test bed right now, it is a simple program that is meant to allow you to register, and do a lot of stupid things via what their book says. (which is headfirstlabs)...

    Attached is the zip of the completed script.

    I keep getting an error...

    login.php's scrip is this:

    
    <?php
      require_once('connectvars.php');
    
    
      if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
        // The username/password weren't entered so send the authentication headers
        header('HTTP/1.1 401 Unauthorized');
        header('WWW-Authenticate: Basic realm="Mismatch"');
        exit('<h3>Mismatch</h3>Sorry, you must enter your username and password to log in and access this page. If you ' .
          'aren\'t a registered member, please <a href="signup.php">sign up</a>.');
      }
    
    
      // Connect to the database
      $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    
      // Grab the user-entered log-in data
      $user_username = mysqli_real_escape_string($dbc, trim($_SERVER['PHP_AUTH_USER']));
      $user_password = mysqli_real_escape_string($dbc, trim($_SERVER['PHP_AUTH_PW']));
    
    
      // Look up the username and password in the database
      $query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND password = SHA('$user_password')";
      $data = mysqli_query($dbc, $query);
    
    
      if (mysqli_num_rows($data) == 1) {
        // The log-in is OK so set the user ID and username variables
        $row = mysqli_fetch_array($data);
        $user_id = $row['user_id'];
        $username = $row['username'];
      }
      else {
        // The username/password are incorrect so send the authentication headers
        header('HTTP/1.1 401 Unauthorized');
        header('WWW-Authenticate: Basic realm="Mismatch"');
        exit('<h2>Mismatch</h2>Sorry, you must enter a valid username and password to log in and access this page. If you ' .
          'aren\'t a registered member, please <a href="signup.php">sign up</a>.');
      }
    
    
      // Confirm the successful log-in
      echo('<p class="login">You are logged in as ' . $username . '.</p>');
    ?>
    
    
    <!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">
    <head>
    <title>Mismatch - Log In</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
    <h3>Mismatch - Log In</h3>
    </body>
    </html>
    
    Code (markup):
    I can't tell what is wrong. line 6 and 7 is this:

    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Basic realm="Mismatch"');


    Any help would be appreciated. I'm about to give up on this because right now for all intended purposes this code should work, but doesn't.

    View attachment noclue.zip
     
    Solved! View solution.
    Last edited: Oct 3, 2011
    Matt Ridge, Oct 3, 2011 IP
  2. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Sorry I forgot the final script to put into the SQL database.

    
    CREATE TABLE `mismatch_user` (
      `user_id` INT AUTO_INCREMENT,
      `join_date` DATETIME,
      `first_name` VARCHAR(32),
      `last_name` VARCHAR(32),
      `gender` VARCHAR(1),
      `birthdate` DATE,
      `city` VARCHAR(32),
      `state` VARCHAR(2),
      `picture` VARCHAR(32),
      PRIMARY KEY (`user_id`)
    );
    
    
    INSERT INTO `mismatch_user` VALUES (1, '2008-06-03 14:51:46', 'Sidney', 'Kelsow', 'F', '1984-07-19', 'Tempe', 'AZ', 'sidneypic.jpg');
    INSERT INTO `mismatch_user` VALUES (2, '2008-06-03 14:52:09', 'Nevil', 'Johansson', 'M', '1973-05-13', 'Reno', 'NV', 'nevilpic.jpg');
    INSERT INTO `mismatch_user` VALUES (3, '2008-06-03 14:53:05', 'Alex', 'Cooper', 'M', '1974-09-13', 'Boise', 'ID', 'alexpic.jpg');
    INSERT INTO `mismatch_user` VALUES (4, '2008-06-03 14:58:40', 'Susannah', 'Daniels', 'F', '1977-02-23', 'Pasadena', 'CA', 'susannahpic.jpg');
    INSERT INTO `mismatch_user` VALUES (5, '2008-06-03 15:00:37', 'Ethel', 'Heckel', 'F', '1943-03-27', 'Wichita', 'KS', 'ethelpic.jpg');
    INSERT INTO `mismatch_user` VALUES (6, '2008-06-03 15:00:48', 'Oscar', 'Klugman', 'M', '1968-06-04', 'Providence', 'RI', 'oscarpic.jpg');
    INSERT INTO `mismatch_user` VALUES (7, '2008-06-03 15:01:08', 'Belita', 'Chevy', 'F', '1975-07-08', 'El Paso', 'TX', 'belitapic.jpg');
    INSERT INTO `mismatch_user` VALUES (8, '2008-06-03 15:01:19', 'Jason', 'Filmington', 'M', '1969-09-24', 'Hollywood', 'CA', 'jasonpic.jpg');
    INSERT INTO `mismatch_user` VALUES (9, '2008-06-03 15:01:51', 'Dierdre', 'Pennington', 'F', '1970-04-26', 'Cambridge', 'MA', 'dierdrepic.jpg');
    INSERT INTO `mismatch_user` VALUES (10, '2008-06-03 15:02:02', 'Paul', 'Hillsman', 'M', '1964-12-18', 'Charleston', 'SC', 'paulpic.jpg');
    INSERT INTO `mismatch_user` VALUES (11, '2008-06-03 15:02:13', 'Johan', 'Nettles', 'M', '1981-11-03', 'Athens', 'GA', 'johanpic.jpg');
    
    Code (markup):
     
    Matt Ridge, Oct 3, 2011 IP
  3. #3
    "output started at /home/pawz/public_html/kaboomlabs.com/editprofile.php:10"

    You can't send headers after sending some HTML already, and editprofile.php starts with HTML.

    You need to send headers before sending HTML.
     
    Foxtr0t, Oct 3, 2011 IP
  4. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    So in other words php before XHTML? Then it should work?
     
    Matt Ridge, Oct 3, 2011 IP
  5. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can NOT sent headers AFTER you 've sent any text output to the browser. It 's like signaling you 'll turn left while you 've already turned right.
     
    jazzcho, Oct 4, 2011 IP
  6. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #6
    I got it working, there was a lot more wrong with it than the basic script, there was a database issue wrong as well, I got it up and working after spending a night blowing things up in Gears 3...

    Sometimes mindless stuff is great to do, it allows you to think about nothing then allows you to go fresh when back to work.

    Thanks for the help though.
     
    Matt Ridge, Oct 4, 2011 IP
  7. propose girl

    propose girl Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    use header at the end.
     
    propose girl, Oct 4, 2011 IP
  8. Foxtr0t

    Foxtr0t Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #8
    It surely is :)
     
    Foxtr0t, Oct 4, 2011 IP
  9. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #9
    Before anything is outputted to the browser, you need to declare this right at the top of your script.

    
    ob_start();
    
    PHP:
    Make sure it's the first piece of code that runs, and this error will not happen no more!

    Glen
     
    HuggyEssex, Oct 4, 2011 IP
  10. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #10
    Before adding it, why does that fix the error? I like to know why I am adding in what I do to make something work so when asked I'll be able to give an educated answer instead of saying, "I dunno."... Thanks :)
     
    Matt Ridge, Oct 5, 2011 IP
  11. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #11
    It fixes it because once you send anything to the browser, you first send headers. (That's done by PHP behind the scenes, so you don't have to do it for every page.) Even a single character triggers headers, so a blank line after the <?php line will do it. ob_start() turns on the output buffer (ob=output buffer), so nothing gets sent until you send the entire buffer, which happens when PHP (the program) finishes processing the page if you haven't flushed the buffer sooner. That sends everything at once, headers, text, everything, so there's no problem.
     
    Rukbat, Oct 7, 2011 IP