-2

Im creating a small basic MVC style website and currently working with my login page. I have three different folders, Model, View and Controller.

So far this is the code i have:

View: Basically just holds the login form and access the controller

   <?php
session_start();
require_once('../Controller/loginController.php');
?>

<!DOCTYPE HTML>

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Energy Checker: Login</title>
</head>

<body>

  <div class="formLogin">
   <h2>Login to your account</h2>
   <form id="loginfrm" method="post">
    <label>Username: </label>
    <input type="text" name ="txtUsername"placeholder="Username" required/>
    <label>Password: </label>
    <input type="password" name ="txtPassword" placeholder="Password"required/>

    <?php
    if(isset($error))
    {
      ?>
      <div class="alert alert-danger">
      </i><?php echo $error; ?> !
    </div>
    <?php
  }
  ?>

  <input type="submit" name="btn-login" value="Login">
</form>
</div>

</body>

</html>

Controller: Just checks if the data has been posted

<?php
require_once('config.php');
require_once('../Model/loginModel.php');

$user = new Login();


if(isset($_POST['btn-login']))
{
    $uname = strip_tags($_POST['txtUsername']);
    $upass = strip_tags($_POST['txtPassword']);

    if($user->getLogin($uname,$upass))
    {
        $user->redirect('../View/calculator.php');
    }
    else
    {
        $error = "Wrong Details !";
    }   
}

?>

Model: Carries out the select statement

    <?php

class Login
{

private $dbconn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->dbconn = $db;
    }

    public function getLogin($uname,$upass)
    {
        try
        {
            $stmt = $this->dbconn->prepare("SELECT * FROM users WHERE Username=:uname");
            $stmt->execute(array(':uname'=>$uname));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($upass, $userRow['Password']))
                {
                    $_SESSION['user_session'] = $userRow['Username'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }
}

?>

I dont receive any errors (anymore)

However when i enter users details it always says "wrong details!"

Not really sure why im getting this

Any help will be appreciated.

T91
  • 135
  • 1
  • 2
  • 9
  • Well it says all! txtUsername is not set and $db must be a property of the class. You can make an init within the constructor of class Login. – B001ᛦ Feb 16 '16 at 13:14
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – David Feb 16 '16 at 13:15
  • So if i include the config file in the model class would that fix the second error? im still not sure about the txtUsername error – T91 Feb 16 '16 at 13:15
  • @T91, the accepted answer is nowhere near to the answer you were looking for. – codisfy Feb 17 '16 at 04:27

1 Answers1

0

The txtUsername has an extra space in the HTML, maybe that is an issue with Undefined index

And for $db you don't have a property called db in the Login class

codisfy
  • 2,155
  • 2
  • 22
  • 32
  • removing the extra space didnt make a difference. – T91 Feb 16 '16 at 13:17
  • Just confirmed name="txtUsername " with a space and name="txtUsername" without a space are indeed different. The one with an extra space comes out as following on print_r ............. Array ( [txtUsername_] => testadmin [txtPassword] => testadminpwd123 [btn-login] => Login ) – codisfy Feb 16 '16 at 13:22
  • Ive changed it to "txtUsername" and i still seem to be getting the error – T91 Feb 16 '16 at 13:24