1

I am taking a class on PHP, and we are learning about object classes. I have been told to create an object class called 'LoginBox' that will validate users and redirect them to a new page depending on whether their information was correct or not. My issue is we have yet to study anything like MYSQL, and that is what most people seem to use for these sorts of problems. I have been told that I am able to use a static username and password for this specific problem, since we have yet to study databases.

My code is as follows. Granted, I clearly am not sure how to even approach this problem, and this code is far from finished and probably beyond saving, but this at least can give you an idea of what I have tried. If anyone can point me in the right direction, I would greatly appreciate the help, as I have been trying to figure this out for hours to no success. I apologize in advance for the mess of code that follows.

class LoginBox {

    public $userName = "user123";
    public $password = "pass123";

    public $var1;
    public $var2;


    public function makeTable() {
        echo '<form action="loggedin.php" method="post">
             Name: <input type="text" name="username"><br>
             E-mail: <input type="text" name="password"><br>
             <input type="submit">
             </form>';
    }

    public function __construct() {
        $this->var1=isset($_POST['username']) ? $_POST['username'] : null;
        $this->var2=isset($_POST['password']) ? $_POST['password'] : null;
    }

    public function SuccessRedirect() {
        if ($var1 = $userName) {
            if ($var2 = $password) {
                echo "Welcome back!";
            }
        }
    }

    public function FailRedirect() {
        if ($var <> $userName) {
            if ($var2 <> $password) {

            }
        }
    }

}
  • 1
    See http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php about making redirects. You need to know the place where to redirect, perhaps create a variable for it (or maybe a argument in constructor?). A change I would make: I’d remove checking the condition in 'FailRedirect' and 'SuccessRedirect' (the check on FailRedirect is incorrect BTW), and just leave the code on unsuccessful/successful attempt. Instead, I’d add a function like ‘doAction’ that will check the condition and call SuccessRedirect or FailRedirect depending on that condition. – Dzmitry Kushnarou Mar 12 '17 at 07:38

1 Answers1

1

your problem is in here:

if ($var1 = $userName) {
//        ^
    if ($var2 = $password) {
//            ^
        echo "Welcome back!";
    }
}

you are assign the $username value to the $var1 , and not compare between them -and same as to $password and $var2-.

to solve this you need to use comparison operators instead of Assignment Operators as follows:

// check -not assign- if $var1 value is equal to $userName value
if ($var1 == $userName) {
    // check -not assign- if $var2 value is equal to $password value
    if ($var2 == $password) {
        echo "Welcome back!";
    }
}

Update

thanks to @DzmitryKushnarou for his notice.

also, you will need to take a look at Logical Operators to make sure whether the username or / and password is correct or not.

for example:

if ($var <> $userName || $var2 <> $password) {

}

another important note, and the thank goes to @Shan.

according to oop nature and variables scopes , you will only be able to call any variable within the function, using $this object, or by passing it to the function as a parameters.

take this as an example: and same as to the FailRedirect method

public function SuccessRedirect() {
    if ($this->var1 == $this->userName) {
        if ($this->var2 == $this->password) {
            echo "Welcome back!";
        }
    }
}

or by passing this parameters to the function as follows:

public function SuccessRedirect($var1, $userName, $var2, $password) {
    if ($var1 == $userName) {
        if ($var2 == $password) {
            echo "Welcome back!";
        }
    }
}

and then pass those parameters values to the function when you are calling it.

Community
  • 1
  • 1
hassan
  • 7,812
  • 2
  • 25
  • 36
  • Good catch! I didn’t notice it. There’s a problem with FailRedirect, too: it will only work if BOTH inputs are invalid, so the situation when just one is valid is left unhandled. – Dzmitry Kushnarou Mar 12 '17 at 07:42
  • 2
    The above code will display undefined variable notices , You need to provide $this->var1, & $this->userName etc – Shan Mar 12 '17 at 07:44
  • @shan good catch ;), i didn't copied the whole function for this purpose. – hassan Mar 12 '17 at 07:45
  • @hassan i understood, but the OP specified that he/she is learning OOP, So you have to do it in a better way. so that he/she can understand very well ;) – Shan Mar 12 '17 at 07:48
  • @DzmitryKushnarou you need to combine the if statements there, `if ($var <> $userName || $var2 <> $password) ....` to check whether the username or password is incorrect – hassan Mar 12 '17 at 07:51
  • @hassan I’m not the OP :P – Dzmitry Kushnarou Mar 12 '17 at 07:52
  • @DzmitryKushnarou I see :D. – hassan Mar 12 '17 at 07:52
  • thanks for your notices, have a nice day, i've updated my answer, thank you. – hassan Mar 12 '17 at 08:02
  • First time posting here, and wow! Did not expect this much help so quickly! I haven't had a lot of practical use with PHP, so a lot of it has yet to sink in, but I'm already seeing a lot of the mistakes I made. You all are awesome! Can't wait to make these changes. Thanks, everyone! – Kurogane789 Mar 12 '17 at 08:35
  • @Kurogane789 you are welcome, and welcome in the php community :) – hassan Mar 12 '17 at 08:39