In order to restrict access to the Bitrix site only for users from Russia, you can configure restrictions at the web server level, for example, using Apache or Nginx, as well as geolocation of IP addresses. One way to do this is using the GeoIP module, or if you use Cloudflare, you can configure access rules through the Cloudflare dashboard. Create a rule to block access for all countries except Russia.
An alternative way (via PHP):
If you only have access to the site folder on Bitrix, and the configuration of the web server (Apache or Nginx) cannot be changed, you can use the country verification code via php It is important to take into account that the verification must be performed before loading any headlines and main content of the site. The ideal place to check is at the very beginning of the file. header.php in the website template, before calls require() and any HTML or PHP constructs that output data to the page.
Verification code using a third-party geolocation API
We will implement country verification using PHP and an API to determine geolocation based on an IP address using the service. ipinfo.io here is an example of how this can be done:
/// We get the current URL of the page $request_uri = $_SERVER['REQUEST_URI']; // If this is the main page (URI == '/'), output the stub if ($request_uri == '/') { echo file_get_contents('https://адрес-сайта/access-denied.html '); exit; } // If not the main one page, checking the IP $ip = $_SERVER['REMOTE_ADDR']; // URL to get information about the country $api_url = "http://ipinfo.io /{$ip}?token=YOUR_TOKEN"; // Registering on http://ipinfo.io / and we get the token, for free we have 50,000 requests // Initialize CURL for request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); // Checking if the API response was successful if ($response === false) { echo'Error receiving country data'; exit; } // Decoding the JSON response $data = json_decode($response, true); // Getting the country code from the response $country_code = $data['country'] ?? "; // If the country is not Russia, remove the stub and stop executing the script if ($country_code !== 'RU') { echo file_get_contents('https://адрес-сайта/access-denied.html '); exit; }
Explanation:
- CURL used for API requests. The parameters are set to receive a response in JSON format.
- The response from the API is decoded via
json_decode, and the country code is extracted ($data['country']). - If the country is not Russia (the country code is not
'RU'), a stub is displayed.
This code will work without saving any user data, and each request will be checked anew.
Result:
- Users with a foreign IP address will see the stub.
access-denied.htmlat the same time, the URL in the address bar will remain unchanged (the main page or any other). - Users from Russia will see a regular website.