Я бы послушал @strager, поскольку ваш код, из-за моего ограниченного опыта PHP, похоже, не показывает ничего, что могло бы вызвать ошибку. Хотя я не могу не предложить несколько простых рефакторингов, не связанных с вашим вопросом, но это только улучшило бы меня :
<?php
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');
require_once(SITE_ROOT.'includes/exceptions.php');
require_once(SITE_ROOT.'data/model.php');
/*
* The purpose of this class is to manage
* access to the application, making sure the
* users are logged in before they can access
* certain features
*/
class Auth extends Model
{
function isUserLoggedIn()
{
/*
* Check for the user_id in $_SESSION
* and see if it's the database. Return
* true or false
*
*/
return isset($_SESSION['user']);
}
static function redirectToLogin()
{
header("location: http://". DOMAIN .APP_DIR . "index.php?action=login");
}
static function redirectToMain()
{
header("location: http://". DOMAIN . APP_DIR . "index.php?action=main");
}
static function login($user)
{
/*
* Authenticate the user passing to the function
* a instance of the User object
*/
$db = parent::getConnection();
$pass = $user->getPassword(); // replaced getPassword in the query with this variable, else there is no need to set it here.
$query = "select username, password from users where username = '".$user->getUsername()."' and password = '".$pass."'";
$results = $db->query($query);
if(empty($results)) {
throw new Exception('There was a problem logging you in', EX_LOGIN_ERROR);
}
$row = $results->fetch_assoc();
$_SESSION['user'] = $row['username'];
// Why bother surrounding with try...catch just to throw the same exception
}
static function logout()
{
// what is $old_user used for? I can't see it set as a global variable anywhere
$old_user = $_SESSION['user'];
unset($_SESSION['user']);
session_destroy();
}
}
?>