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
  • 1
    See stackoverflow.com/questions/768431/… 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. Commented Mar 12, 2017 at 7:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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.
The above code will display undefined variable notices , You need to provide $this->var1, & $this->userName etc
@shan good catch ;), i didn't copied the whole function for this purpose.
@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 ;)
@DzmitryKushnarou you need to combine the if statements there, if ($var <> $userName || $var2 <> $password) .... to check whether the username or password is incorrect
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.