I'm building a website that works like a console. I have a status script that check if the page is offline or you're blacklisted/whitelisted. I do this by look up the IP in my database. Then after that I use a switch to direct the user. I use javaScript to redirect because the site uses ajax and I can't get header('Location: xxx'); to work with it(A problem for another time).
When I run the site with the status page it loads twice as slow, and it doesn't help that page loads this script each time the user uses a command.
Do you have any suggestions on how to optimize it to load faster? I´m open for all ideas. Thanks for your time.
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
};
$conn = new MySQLi('localhost', 'xxx', 'xxx', 'xxx');
$statusRank = "";
$sql = "SELECT enable FROM global WHERE enable = '1' AND setting = 'frontend'";
$query = $conn->query($sql) or die ($conn->error);
if(mysqli_num_rows($query)) {
$statusRank = "offline";
}
$sql = "SELECT ip FROM blacklist WHERE ip = '$ip'";
$query = $conn->query($sql) or die ($conn->error);
if(mysqli_num_rows($query)) {
$statusRank = "blacklist";
}
$sql = "SELECT ip FROM whitelist WHERE ip = '$ip'";
$query = $conn->query($sql) or die ($conn->error);
if(mysqli_num_rows($query)) {
$statusRank = "whitelist";
}
switch ($statusRank) {
case "blacklist":
?><script>window.location.replace("include/blacklist.php");</script><?php
break;
case "whitelist":
break;
case "offline":
?><script>window.location.replace("include/offline.php");</script><?php
break;
default:
};