/    Sign up×
Community /Pin to ProfileBookmark

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

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

Thanks in advance!

[CODE]$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();[/CODE]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @cgm225 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...