0

I've been trying to make a login page with 000webhost, and this error keeps popping up, and i know there are tons of these king of questions everywhere but i still cant fix it

this is the error

Notice: Undefined index: active in /storage/ssd1/326/5052326/public_html/login.php on line 14

Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd1/326/5052326/public_html/login.php:14) in /storage/ssd1/326/5052326/public_html/login.php on line 23

and this is the code

    <?php
   include("config.php");
   session_start();

      // username and password sent from form 
    if($_SERVER["REQUEST_METHOD"] == "POST") {

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT * FROM admin WHERE username = '$myusername' and password = '$mypassword'";
      $result = mysqli_query($db, $sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         $_SESSION['login_user'] = $myusername;

         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
    }
?>
syam
  • 799
  • 1
  • 12
  • 30
  • 2
    Looks like your database table `admin` does not have a column called `active`. It's also possible that the table has a column called `active` but no rows are returned when the SQL query is run. – Casper André Casse Mar 13 '18 at 10:46

1 Answers1

1

Make sure - at line 14 -> $row['active']; has value before calling

header("location: welcome.php");

to prevent header error message.

You cannot output anything before a header() call. It will cause an error.

You can do:

$active = isset( $row['active'] ) == TRUE ? $row['active'] : '0';

Replace '0' with your value. That should prevent the error.

Karlo Kokkak
  • 3,674
  • 4
  • 18
  • 33