I would like to output an error message on login if the website is under maintenance however my current code doesn't work and seems to just run as if the maintenance code isn't there. I would like it so if the maintenance column in MySQL database which i have already defined as $maintenance
is empty then the user can login like normal however if it contains 1
then the user will see the error message however admins with their IP in the array can still login. I have defined $maintenance
in a different file which is included already in my class.user.php
. Code is below.
Settings.php
$auth_user = new USER();
$site_name = $auth_user->runQuery("SELECT * FROM `settings` LIMIT 1");
$site_name->execute();
while ($show = $site_name -> fetch(PDO::FETCH_ASSOC)){
$maintenance = $show['maintenance'];
}
Class.user.php
require_once('settings.php');
....other functions here
....other functions here
.....other functions here
.....
public function doLogin($uname,$umail,$upass)
{
try
{
$stmt = $this->conn->prepare("SELECT user_id, user_name, user_email, user_pass, status FROM users WHERE user_name=:uname OR user_email=:umail ");
$stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() == 1)
{
if(password_verify($upass, $userRow['user_pass']))
{
session_regenerate_id(false);
return ["correctPass"=>true, "banned"=> ($userRow['status']== 1) ? true : false, "maintenance"=> ($maintenance== 1) ? true : false];
}
else
{
return ["correctPass"=>false];
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
Login.php
$validation = $login->doLogin($uname,$umail,$upass);
if($validation["correctPass"]){
if($validation["maintenance"]){
if (!in_array(@$_SERVER['REMOTE_ADDR'], array('1.1.1.1'))){
$error = "Website under maintenance";
}
}
if($validation["banned"]){
$error = "User has been banned";
}else{
if(Token::check($_POST['token'])) {
$stmtt = $login->runQuery("SELECT user_id FROM users WHERE user_name=:uname OR user_email=:umail ");
$stmtt->execute(array(':uname'=>$uname, ':umail'=>$umail));
$userRow=$stmtt->fetch(PDO::FETCH_ASSOC);
$_SESSION['user_session'] = $userRow['user_id'];
$success = "Logged in successfully, redirecting..";
header( "refresh:3;url=dashboard" );
} else {
$error = "Unexpected error occured";
}
}
}
else{
$error = "Incorrect username/email or password";
}