I'm in the process of building a small application and obviously I need some sort of way to authenticate users. I'm not sure if I am way off or if this is close to what I should be thinking. Please let me know what you think...
<?php
class Authentication_controller {
private __constructor() {
include(Authentication_model.php);
$auth_model = new Authentication_Model();
$auth_model->database_connect();
}
public login($username, $password) {
if($auth_model->username($username)) {
//Username exists, now check password
if($auth_model->password($username, $password)) {
//User is OK to login, send to designated page
include(Page_controller.php);
$page_controller = new Page_controller();
$page_controller->go('home');
//Create session
session_start();
//Set session variables
$_SESSION['logged_in'] = true;
$_SESSION['id'] = $auth_model->id($username);
$_SESSION['access_level'] = $auth_model->access_level($username);
} else {
//Password didn't match
return $error = "Username or password incorrect."
}
} else {
//Username didn't match
return $error = "Username or password incorrect."
}
}
public permission($task_access_level) {
$user_access_level = $_SESSION['access_level'];
if($user_access_level >= $task_access_level) {
//User has access level that is high enough for task
return true;
} else {
//User does not have access level high enough for task
return false;
}
}
public logout() {
session_destroy();
}
}
Is this typically how a class like this should work? I'm still very new to this stuff.
require_once
rather thaninclude
- for onerequire_once
performs better in that it throws a fatal error if the file is not found and stops the script (rather than letting it continue as include_once or include would, and for two, it checks that the file has not already been included and if it has been included will not require it again. Mind that require() executes faster than require_once(), so if you're sure there are no duplicated includes use require(). \$\endgroup\$