diff --git a/Readme.md b/Readme.md index f9ba9ca..dc6c753 100644 --- a/Readme.md +++ b/Readme.md @@ -75,26 +75,6 @@ only:Malaysia --- -## Example Output -### Success -```html -
Your access is granted.
-``` -### Restricted Access (VPN) -```html -
Access Denied: VPNs are not allowed on this website.
-``` -### Restricted Access (Country) -```html -
Access Denied: Visitors from India are restricted from accessing this website.
-``` -### Error -```html -
Error: Unable to determine your location. Access granted by default.
-``` - ---- - ## Instructions for Use 1. Add the `access_control.php` script to your PHP project. 2. Create and configure the `access.txt` file with your desired rules. diff --git a/access_control.php b/access_control.php index 397be14..5f590df 100644 --- a/access_control.php +++ b/access_control.php @@ -1,21 +1,30 @@ 'fail', 'message' => 'API error']; + } + return json_decode($response, true); } -// Function to parse access rules from access.txt +function checkAccess() { + include "access_control.php"; // Run access checks +} + +// Function to parse access rules function getAccessRules($filePath) { - $rules = [ - 'allow' => [], - 'block' => [], - 'only' => [], - ]; + $rules = ['allow' => [], 'block' => [], 'only' => []]; if (!file_exists($filePath)) { - return $rules; // Return empty rules if file doesn't exist + return $rules; } $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); @@ -32,10 +41,8 @@ function getAccessRules($filePath) { return $rules; } -// Get the client's IP address +// Get client IP $clientIp = $_SERVER['REMOTE_ADDR']; - -// Fallback for IP detection if server is behind a proxy if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $clientIp = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]; } @@ -43,46 +50,104 @@ function getAccessRules($filePath) { // Fetch geolocation and VPN data $geoData = getGeolocationData($clientIp); -// Handle errors gracefully +// Handle API errors if ($geoData['status'] !== 'success') { - echo "
Error: Unable to determine your location. Access granted by default.
"; - return; + showBlockPage("Warning", "Unable to determine your location. Access granted by default.", "orange", "fa-exclamation-triangle", ""); } -// Extract country and proxy information +// Extract country and VPN status $country = $geoData['country'] ?? 'Unknown'; +$countryCode = $geoData['countryCode'] ?? 'UN'; $isVpn = $geoData['proxy'] ?? false; +$flagUrl = "https://flagsapi.com/{$countryCode}/flat/64.png"; // Load access rules -$accessRules = getAccessRules('access.txt'); +$accessRules = getAccessRules(__DIR__ . '/access.txt'); // VPN blocking logic if ($isVpn) { - echo "
Access Denied: VPNs are not allowed on this website.
"; - exit; + showBlockPage("Access Denied", "VPNs are not allowed on this website.", "red", "fa-ban", $flagUrl); } // "only" access logic -if (!empty($accessRules['only'])) { - if (in_array($country, $accessRules['only'], true)) { - echo "
Access Granted: Welcome, User from {$country}.
"; - return; - } else { - echo "
Access Denied: Only {$country} are allowed on this website.
"; - exit; - } +if (!empty($accessRules['only']) && !in_array($country, $accessRules['only'], true)) { + showBlockPage("Access Denied", "Only visitors from selected countries are allowed.", "red", "fa-times-circle", $flagUrl); } -// Block/allow logic +// Block logic if (in_array($country, $accessRules['block'], true)) { - echo "
Access Denied: Visitors from {$country} are restricted from accessing this website.
"; - exit; + showBlockPage("Access Denied", "Visitors from {$country} are restricted.", "red", "fa-times-circle", $flagUrl); } +// Allow logic if (!empty($accessRules['allow']) && !in_array($country, $accessRules['allow'], true)) { - echo "
Access Denied: Visitors from {$country} are not in the allowed countries list.
"; - exit; + showBlockPage("Access Denied", "Visitors from {$country} are not in the allowed list.", "red", "fa-times-circle", $flagUrl); } -echo "
Access Granted: Welcome, Visitor from {$country}.
"; +// ✅ If access is granted, continue loading the main page +return; + +// Function to display block page and stop execution +function showBlockPage($title, $message, $color, $icon, $flagUrl) { + echo " + + + $title + + + + +
+ +

$title

+

$message

+ " . ($flagUrl ? "Flag" : "") . " +
+ + "; + exit; +} ?>