POO PHP - Undefined index: user and password

2
  

Notice: Undefined index: user in   C: \ xampp \ htdocs \ DSagredo \ inc \ functions.php on line 18

     

Notice: Undefined index: password in   C: \ xampp \ htdocs \ DSagredo \ inc \ functions.php on line 19

INDEX.PHP

$obj->getLogueo();  

if (isset($_GET["m"]) && !empty($_GET["m"])) 
{
    $error_msg = "<div class='error'>Usuario o contraseña incorrectos</div>";
}

include 'inc/header.php';
?>
<div class="caja">

    <div class="foro">
        <form method="post" action="#">
        <?php echo $error_msg; ?>
        <h2>Iniciar sesión</h2>
        <input type="text" name="user" id="user" placeholder="Usuario" required autofocus>
        <input type="password" name="password"  placeholder="Contraseña" required>
        <br>
        <button class="btn" type="submit">Entrar</button>
      </form>

    </div>
</div>

FUNCTION.PHP

public function getLogueo() 
   {
      $user = $_POST["user"];
      $pass = md5($_POST["password"]);

      $res = $this->mysqli->query("SELECT id, name, kind from user where user = '$user' and password = '$pass'");

      while ($row = $res->fetch_assoc()) 
      {
         $this->login[] = $row;
      }

      if (sizeof($this->login) > 0) 
      {
         foreach ($this->login as $key) 
         {
            $_SESSION["id"] = $key["id"];
            $_SESSION["name"] = $key["name"];
            $_SESSION["kind"] = $key["kind"];
            switch ($_SESSION["kind"]) 
            {
               case 1: header("Location: panel.php");
               break;
               case 2: header("Location: index2.php");
               break;
            }
         }
      } else {
         header("Location: index.php?m=1");
      }
   }
    
asked by Diego Sagredo 15.09.2016 в 15:34
source

1 answer

1

If you call the elements of the variable $_POST before sending any form through POST, those indexes in the array will not be available.

You are calling the getLogueo function at the beginning

$obj->getLogueo(); 

Which has:

$user = $_POST["user"];
$pass = md5($_POST["password"]);

So if you open the page that calls that function, these elements will not be available.

To fix it you could use the isset() function to check that they are set correctly, you could do something like that.

public function getLogueo() {
  if(isset($_POST["user"]) && isset($_POST["password"])){
    $user = $_POST["user"];
    $pass = md5($_POST["password"]);

    $res = $this->mysqli->query("SELECT id, name, kind from user where user = '$user' and password = '$pass'");

    while ($row = $res->fetch_assoc()){
      $this->login[] = $row;
    }

    if (sizeof($this->login) > 0) {
      foreach ($this->login as $key) {
        $_SESSION["id"] = $key["id"];
        $_SESSION["name"] = $key["name"];
        $_SESSION["kind"] = $key["kind"];
        switch ($_SESSION["kind"]) {
           case 1: header("Location: panel.php");
           break;
           case 2: header("Location: index2.php");
           break;
        }
      }
    } else {
     header("Location: index.php?m=1");
    }
  }else{
    echo "No se ha enviado nada por post";
  }
}
    
answered by 15.09.2016 / 15:45
source