Jump to content

How would you do this class differently.. I just don't like my organization...


cgm225

Recommended Posts

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

 

Thanks in advance!

 


$mysqli = new mysqli(MYSQL_SERVER,MYSQL_SERVER_USERNAME,MYSQL_SERVER_PASSWORD);

class Authenication {
    
    //Declaring variables
    private $username;
    private $password;
    private $connection;
    
    //Setting username and password
    public function setUsernamePassword($username, $password) {
        $this->username = mysql_real_escape_string($username);
        $this->password = md5(mysql_real_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 Authenication();
$authentication->setUsernamePassword('user1','pass1');
$authentication->databaseSettingsQuery($mysqli, 'db_authentication', 'users', 'username', 'password');
$authentication->Authorization();


$mysqli->close();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.