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. We have yet to study MYSQL, so I am unable to use that for this example. 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 problem is that I don't know how to carry info from one page to another using an Object class. I feel like if I can get that figured out, my class will do just about everything I need it to do, but its hard to tell for sure since this part is preventing me from seeing the results. As you can see in my code, I try to use $_Post, but now matter what I try the information does not carry over to the new page, and it displays nothing. Does anyone have any suggestions on how to tackle this problem? Thanks in advance!

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 $SuccessRedirect = "Welcome back!";

    public $FailRedirect = "You have entered the incorrect information. Please return and try again.";

    public function action() {
        if ($this->$var1 <> $this->$userName || $this->$var2 <>$this->$password) {
            echo $failRedirect;
        }
        else {
            echo $SuccessRedirect;
        }
    }
}

2 Answers 2

1

I have corrected your script:

class LoginBox {

    public $userName = "user123";
    public $password = "pass123";
    public $var1;
    public $var2;
    public $SuccessRedirect = "Welcome back!";
    public $FailRedirect = "You have entered the incorrect information. Please return and try again.";

    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 action() {
        if ($this->userName && $this->password && $this->var1 == $this->userName && $this->var2 == $this->password) {
            echo $this->SuccessRedirect;
        } else {
            echo $this->FailRedirect;
        }
    }
}

$cl = new LoginBox();
$cl->makeTable();
$cl->action();

You should compare your version and mine to understand all the changes I made. Your main issue was that you used $this->$varname instead of $this->varname.

Variables should be declared on top of your class.

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

2 Comments

Thank you very much! Its working now. I'll have to be sure to watch out for $this. I'm still not used to using it with classes, but thank you for pointing it out.
Sure. Also, if you don't use a var outside the class itself, you should declare it as private $var instead of public $var
1

Just want to point out a few things.

  1. You use an invalid "not equal", in PHP, it should be != instead of <>.

  2. It should be $this->var instead of $this->$var.

  3. Since you are learning creating and using object-oriented Class, so you must have heard about the concept of 'encapsulation'. If you declare everything within the class as public, anyone can access it from outside of the class. It should be declare as private. Certainly not for $usernme and $password. Just try to echo (new LoginBox)->password to see yourself.

  4. It is in general not a good practice for a Class to handle presentation (i.e. echo out data directly), it should return data instead on your action() method. The program that calling can then handle the display of the status like so echo class->action(); or $status=class->action().

Hope this will provide some good foundation concepts for your learning.

Comments

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.