Hi all. I have an assignment i'm working on but I am getting some errors now. The page it is coming from is a login page. The customer has signed up and now i want them to login and store their data in a session. heres the errors

Warning: Cannot modify header information - headers already sent by (output started at /home/ssharples/public_html/Football shop/login.php:3) in /home/ssharples/public_html/Football shop/login.php on line 50

Warning: Cannot modify header information - headers already sent by (output started at /home/ssharples/public_html/Football shop/login.php:3) in /home/ssharples/public_html/Football shop/login.php on line 51

Warning: Cannot modify header information - headers already sent by (output started at /home/ssharples/public_html/Football shop/login.php:3) in /home/ssharples/public_html/Football shop/login.php on line 53

and heres the code from line 20, so the errors are 31,32 and 34.

<?
 

ob_start();

 

  // Clear the error message
  $error_msg = "";

  // If the user isn't logged in, try to log them in
  if (!isset($_SESSION['user_id'])) {
    if (isset($_POST['submit'])) {
      // Connect to the database
      $dbc = mysqli_connect("localhost", "ssharples", "te2888", "ssharplesdb");

      // Grab the user-entered log-in data
      $user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
      $user_password = mysqli_real_escape_string($dbc, trim($_POST['password']));

      if (!empty($user_username) && !empty($user_password)) {
        // Look up the username and password in the database
        $query = "SELECT user_id, username FROM 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 session vars (and cookies), and redirect to the home page
          $row = mysqli_fetch_array($data);
          $_SESSION['user_id'] = $row['user_id'];
          $_SESSION['username'] = $row['username'];
          setcookie('user_id', $row['user_id'], time() + (60 * 60 * 24 * 30));    // expires in 30 days
          setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
          $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
          header('Location: ' . $home_url);
        }
        else {
          // The username/password are incorrect so set an error message
          $error_msg = 'Sorry, you must enter a valid username and password to log in.';
        }
      }
      else {
        // The username/password weren't entered so set an error message
        	$error_msg = 'Sorry, you must enter your username and password to log in.';
      }
    }
  }
  
  ob_flush() ;
?>



        
<?php
          // If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in
          if (empty($_SESSION['user_id'])) {
            echo '<p class="error">' . $error_msg . '</p>';
        ?>

all help is gratly appreciated. thank you

Recommended Answers

All 2 Replies

The reason is because you are setting cookie info, this will send the header to the browser, causing the error you are getting.

Now my question to you is why are you even saving that information in a cookie, it is the same information you are saving in the session?

start the session before any output is sent.
if none sent then check maybe it's a whitespace it counts as an output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.