How would you do this class differently..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kilbad
Forum Commoner
Posts: 28
Joined: Wed Apr 02, 2008 3:51 pm

How would you do this class differently..

Post by kilbad »

How would you organize/code this class differently. I just don't feel like my organization is very good.

Thanks in advance!

Code: Select all

$mysqli = new mysqli(MYSQL_SERVER,MYSQL_SERVER_USERNAME,MYSQL_SERVER_PASSWORD);
 
class Authentication {
    
    //Declaring variables
    private $username;
    private $password;
    private $connection;
    
    //Setting username and password
    public function setUsernamePassword($username, $password) {
        $this->username = mysql_escape_string($username);
        $this->password = md5(mysql_escape_string($password));
    }
    
    /*
        Passing MySQL connection, database, table, and field information to
        the class, all used to generate a database query for finding a matching
        username and password in the table
    */
    public function databaseSettingsQuery($connection, $database, $table, $usernameField, $passwordField) {
        $connection->select_db($database);
        $this->result = $connection->query("SELECT * FROM $table WHERE $usernameField = '$this->username' AND $passwordField = '$this->password'");
    }
    
    /*
        Results of the query are counted and, if greater than zero,
        $this->authorized is set to true and the provided username and password
        are passed to the setSession method
    */
    public function Authorization() {
        $this->authorized = $this->result->num_rows > 0 ? TRUE : FALSE;
        $this->result->close();
        
        if ($this->authorized) {
            $this->setSession($this->username, $this->password);
        }
    }
    
    //Setting the provided username and password to session variables
    public function setSession($username, $password) {
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
    }
}
 
 
$authentication = new Authentication();
$authentication->setUsernamePassword('user1','pass1');
$authentication->databaseSettingsQuery($mysqli, 'db_authentication', 'users', 'username', 'password');
$authentication->Authorization();
 
 
$mysqli->close();
Post Reply