Couple of questions about this code

Discussion in 'PHP' started by scottlpool2003, Oct 24, 2012.

  1. #1
    1. The blank value of password="" and password2="" returns "Passwords do not match", but if I enter a value such as password="a" password2="a" it returns "Passwords do match" why does the script not read "" as a value? Even if I add a space it still doesn't match?

    2. How can I declare a password after the class/function? I've tried all sorts but I can't seem to declare a new password.
    <?php
    
    class Login {
    
        public $username;
        public $password;
        public $password2;
    
    function Login(){
    
        $this->username = "";
        $this->password = "";
        $this->password2 = "";
        if ($this->password = $this->password2){
            echo "Passwords match!";
        }else {
            echo "Passwords do not match";
        }
    }
    
    }
    
    PHP:
    
    
    //doesn't work here
    $username = "user";
    $password = "pass";
    $password2 = "pass";
    
    //doesn't work here
    $this->password = "pass";
    $this->password2 = "pass";
    
    $Login = new Login();
    
    //doesn't work here
    $username = "user";
    $password = "pass";
    $password2 = "pass";
    
    //doesn't work here
    $this->password = "pass";
    $this->password2 = "pass";
    
    ?>
    
    
    PHP:

     
    scottlpool2003, Oct 24, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #2
    you have your logic a bit wrong. try this.

    
    
    class Login {
    
        public $username;
        public $password;
        public $password2;
    
    function logMeIn(){
    
        $this->username = "";
        $this->password = "";
        $this->password2 = "";
        if ($this->password == $this->password2){
            echo "Passwords match!";
        }else {
            echo "Passwords do not match";
        }
    }
    
    }
    
    
    $login = new Login();
    $login->password = "pass";
    $login->password2 = "pass";
    
    $login->logMeIn();
    
    PHP:
     
    plussy, Oct 24, 2012 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Thanks, same problem though. Passwords always match if I change the values of:

    $login->password = "pass";
    $login->password2 = "pass";
    PHP:

    And the script only works if I change the values of:

    $this->password = "";
    $this->password2 = "";
    PHP:
     
    scottlpool2003, Oct 24, 2012 IP
  4. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #4
    sorry missed something


    
    
    class Login {
    
        public $username;
        public $password;
        public $password2;
    
    function logMeIn(){
    
        
        
        if ($this->password == $this->password2){
            echo "Passwords match!";
        }else {
            echo "Passwords do not match";
        }
    }
    
    }
    
    
    $login = new Login();
    $login->password = "pass";
    $login->password2 = "pass";
    
    $login->logMeIn();
    
    
    PHP:
     
    plussy, Oct 24, 2012 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5
    Worked a treat, thanks.

    So if I were to make this work using a database class, could I use something like:

            if ($this->password == $this->password2){
                $this->dbcheck();
            }else {
                echo "Passwords do not match";
            }
    PHP:
    And have dbcheck(); query the database using a separate function?
     
    scottlpool2003, Oct 24, 2012 IP
  6. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #6
    yes you could do that. But I would not echo anything. I would simply return false or an error message but let the other code handle any outputs.

    so something like this


    
    
    class Login {
    
        public $username;
        public $password;
        public $password2;
    
    function logMeIn(){
    
        
        
        if ($this->password == $this->password2){
            return "Passwords match!";
        }else {
            return "Passwords do not match";
        }
    }
    
    }
    
    
    $login = new Login();
    $login->password = "pass";
    $login->password2 = "pass";
    
    $result = $login->logMeIn();
    
    echo $result;
    
    
    PHP:
    I would (whenever possible) avoid having objects echo stuff but rather return content. This will give you greater flexibility.
     
    plussy, Oct 24, 2012 IP
  7. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #7
    Ok thanks, I appreciate your help.

    So further more, would I store all classes in one file or would I split them like loginclass.php databaseclass.php etc. Also, if I changed loginclass.php to accounthandler.php and store all classes/functions relating to user accounts such as login, logout, register, closeaccount would this be better practice?

    Lastly, in your last code example, where and how would I declare the database check if the passwords are the same? Would it need to be in a separate file or could I write the function in instead of echoing like so:

    
    <?php
    
    dbconnect();
    
    class Login {
    
        //Declare variables
        public $username;
        public $password;
        public $password2;
    
        //Start login function
        function logMeIn(){
    
            //Check if passwords match
            if ($this->password == $this->password2){
                
    //Match user in db
    //Code the function rather than include the class here
    
            }else {
                die("Passwords do not match");
            }
        }
    
    }
    
    ?>
    
    PHP:
     
    scottlpool2003, Oct 24, 2012 IP
  8. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #8
    These are all good questions and I don't have definite answers to all of them but here is what I do.

    for example I create a file call user.class.php and put it into a folder called classes or includes

    In this user class I will have any function that has to deal with a user account so for example

    getUserById()
    getUserPermissions()
    updateUser()
    createUser()
    doesUserExist()

    and so on.

    You just need to make sure that the structure is easy to follow and that you are consistent. Also don't make your live harder than it needs to be. Keep it simple. The easier everything is the easier it is to debug the code if something goes wrong or if someone else needs to work on it.

    hope this helped.
     
    plussy, Oct 24, 2012 IP
  9. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #9
    It did thanks, good job you joined you've had some really good input over the last 2 months. Keep it up, DP is missing people like you.
     
    scottlpool2003, Oct 24, 2012 IP
  10. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #10
    Thanks. Glad to help my follow coders ;)
     
    plussy, Oct 24, 2012 IP