Share:

How to Block Visitors from a Country from Accessing My Website

كيف أحظر زوار دولة من الدخول لموقعي الالكتروني

Sometimes we may want to block visitors from an entire country from accessing our website for various reasons. These include security reasons or an attack from certain bots coming from a specific country.

Today, we will learn the best and fastest ways to block visitors from a certain country from accessing our website.

Related: Best Browsers to Open Blocked Sites

Why You Might Need to Block Visitors from a Certain Country?

Blocking visitors from an entire country may seem like a drastic step at first, but it is often a necessary security and regulatory measure to protect your website and its stability.

One of the most significant reasons that drive website owners to take this step is the noticeable rise in fake traffic or bot visits that provide no real value but consume server resources.

More importantly, a massive influx of fake visits from specific countries can mislead advertising network algorithms like Google AdSense, resulting in views with a zero profit rate, and potentially exposing your account to the risk of suspension due to suspected invalid clicks or interactions.

Therefore, geographic blocking becomes an effective means to filter traffic and focus on the real target audience.

Here are the best and fastest methods available to implement this block, ranked by suitability.

Check out: Easiest Website to Access Blocked Sites for Free Without Programs in 2026

1. Blocking via Cloudflare Service

If your website is linked to the Cloudflare service (which is free and offers excellent protection), you can easily block the targeted country in just a few steps without putting pressure on your server:

  1. Log in to your Cloudflare dashboard.
  2. Select your website domain.
  3. From the sidebar, go to Security. Then select Security Rules.
  4. In the upper right corner, click on Create rule+ and then select Custom rules.
  5. In the Rule name field, name your rule (e.g.: Block Israel or Israel) or block the state entity.
  6. In the Field box, select Country.
  7. In the Operator field, choose Equals.
  8. In the Value field, choose Israel.
  9. In the Choose action field, choose Block. And note there are 5 crucial options because the block option must be the last choice; we will explain that later.
  10. Click Deploy to start the block.

Explanation of Blocking Options Based on Visitor’s Geographic Location

Here are explanations of the available Choose action options in Cloudflare that will be taken against visits from the specified country:

  • Block: This is the required option for our case. It prevents the visitor from accessing the website entirely and displays an error page indicating that access is blocked.
  • Managed Challenge: Cloudflare automatically evaluates the visitor; if they are a real human browser, they are quietly let in, and if there’s suspicion that it’s a bot or a suspicious search engine, a verification test (like a light Captcha) appears to ensure they are human.
  • Interactive Challenge: Forces any visitor coming from this country to solve a visual verification test (like clicking on images or verification codes) before being allowed to enter the site.
  • Non-Interactive Challenge: Checks the visitor’s browser and behavior in the background without disturbing humans, and if any automated behavior is detected, it is blocked immediately.
  • Skip: Used if you have general rules that you want to exclude this country or condition from.

Read also: How to Change Your Geolocation and IP for Free on iPhone Without VPN Software

2. Blocking through the .htaccess File

This method (if your site runs on an Apache/cPanel server)

If you are not using Cloudflare and want to block visits through the hosting server directly, you can use codes based on country codes (GeoIP).

Note: This method requires the GeoIP library to be enabled on your hosting server.

Open the .htaccess file located in your website’s main folder and add the following code:

Apache

<IfModule mod_geoip.c>
SetEnvIf GEOIP_COUNTRY_CODE SG BlockCountry
Deny from env=BlockCountry
</IfModule>

Replace the code SG, which represents Singapore in our example, with the code of the country you wish to block.

3. Blocking Visitors from a Country on My WordPress Site

If your site is designed with WordPress, you can use custom plugins to manage security or geographic blocking easily from the dashboard:

  • Install either of the two plugins iThemes Security or Wordfence Security: These plugins have geographic blocking features based on the IP of the country from which the visits come (Country Blocking).

There are specific blocking plugins such as IP Geo Blog or Country Blocker.

Blocking Visitors from a Country Using PHP Code

To block using PHP and rely on free libraries to determine the country via the visitor’s IP address, you can use the free MaxMind GeoLite2 libraries (which require downloading a database in .mmdb format), or rely on ready-made light free APIs that do not require storing massive files on the server.

Here’s the easiest and fastest way to block visitors from a country using a free API (like ipapi or ip-api) that determines the country directly based on the IP without needing to download heavy databases.

This is the PHP code to block visitors from a country but of course, replace the code SG with the code of the country you want to block. (But where to place the code? Follow along)

<?php // Function to get the visitor's real IP address function get_client_ip() { $ipaddress = ''; if (isset($_SERVER['HTTP_CLIENT_IP'])) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_X_FORWARDED'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_FORWARDED'])) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if(isset($_SERVER['REMOTE_ADDR'])) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; return $ipaddress; } $visitor_ip = get_client_ip(); // Exclude localhost or local IP during testing if ($visitor_ip != '127.0.0.1' && $visitor_ip != '::1') { // Use a free service to get country information (like ip-api) $response = @file_get_contents("http://ip-api.com/json/{$visitor_ip}?fields=status,countryCode"); if ($response) { $data = json_decode($response, true); if (isset($data['status']) && $data['status'] == 'success') { $country_code = $data['countryCode']; // If the country is Singapore (code SG) block it if ($country_code === 'SG') { header("HTTP/1.1 403 Forbidden"); echo "Access Denied: You are not allowed to view this site."; exit(); } } } } ?>

For WordPress Sites

If you own WordPress: You can place this code at the beginning of your site’s main header file (like header.php or the main entry file).

For General Sites (WordPress and others)

Create a file named something like block_sg.php and put the previous code in it. You have two options to ensure that the code is executed on all pages of the site:

First option: If your site runs on an Apache server, you can make the server execute this file automatically in the background of every requested PHP page without needing to modify any core programming files. Add the following line to the .htaccess file in the root of the site:

php_value auto_prepend_file "/path/to/your/block_sg.php"

(Note: Replace the above path with the actual full path of the file on the server).

Second option: If your site relies on a single main entry file through which all requests go (like index.php or a general configuration file called at the beginning of every page), it is sufficient to place just one line at its beginning:

include_once 'block_sg.php';

These methods make the blocking process entirely centralized and outside of design or header files.

You might be interested in: Know your IP Address


أكثر هواتف آيفون مبيعاً

إعلان الهاتف

آيفون 16 128 جيجابايت

آيفون 16 128 جيجابايت

يُقدم هاتف iPhone 16 تجربة ممتازة تجمع بين الأداء القوي والتصميم العملي، مدعوماً بمعالج A18 المصمم خصيصاً لدعم مهام الذكاء الاصطناعي (Apple Intelligence). ويأتي بشاشة Super Retina XDR زاهية، إلى جانب زر التحكم بالكاميرا الجديد (Camera Control) الذي يمنح المستخدمين وصولاً سريعاً وسلساً لالتقاط الصور والفيديوهات.

ملخص آراء أشهر المواقع التقنية

"معالج A18 القوي يوفر سرعة هائلة وأداءً سلساً للغاية في تشغيل التطبيقات والألعاب الحديثة دون أي تأخير."

TrustedReviews
★★★★★

"زر التحكم في الكاميرا الجديد يضيف لمسة عملية رائعة ومبتكرة تسرع من عملية التصوير باحترافية."

TechRadar
★★★★☆

"عمر البطارية شهد تحسناً ملحوظاً مقارنة بالأجيال السابقة ليصمد بكفاءة طوال اليوم."

The Verge
★★★★★

المميزات

  • معالج Apple A18 القوي الذي يضمن كفاءة عالية في استهلاك الطاقة ودعم كامل لتقنيات الذكاء الاصطناعي.
  • زر التحكم الجديد في الكاميرا (Camera Control) للوصول السريع والتحكم الدقيق بالإعدادات والتصوير.
  • شاشة Super Retina XDR ساطع وعالية الوضوح تقدم ألواناً دقيقة ورائعة.
  • نظام كاميرا خلفية مزدوج محسن بدقة 48 ميجابكسل مع عدسة فائقة الاتساع تدعم التصوير الماكرو.
  • زر الإجراءات (Action Button) القابل للتخصيص لتنفيذ المهام السريعة بضغطة واحدة.

العيوب

  • الشاشة لا تزال تقتصر على معدل تحديث 60 هرتز، وهو أمر مخيب للآمال مقارنة بالهواتف المنافسة في نفس الفئة السعرية.
  • سرعات الشحن السلكي واللاسلكي تعتبر بطيئة نسبياً مقارنة بما تقدمه الهواتف المنافسة في السوق.
  • التصميم الخارجي والشكل العام يشبهان إلى حد كبير الجيل السابق مع تغييرات طفيفة تتركز في تصميم الكاميرا الخلفية.

مواصفات الهاتف

اسم المنتج بالعربيآيفون 16
اسم المنتج بالإنجليزيiPhone 16
تاريخ التوفر20/09/2024
الشركة المصنعةapple
نظام التشغيلiOS 18, قابل للتحديث لـ iOS 26.5
المعالجApple A18 3 نانومتر Hexa-core (2x4.04 GHz + 4x2.20 GHz)
ذاكرة RAM8GB
سعة التخزين128GB
حجم الشاشة6.1 بوصة
الكاميرا الخلفية48 ميجابكسل f/1.6
الكاميرا الأمامية12 ميجابكسل f/1.9
الصوتستيريو
وصلة سماعة 3.5غيرمتوفر
البطاريةLi-Ion 3561 ميللي أمبير
الشحن اللاسلكيمدعوم 25W
الألوان المتوفرةأسود/أبيض/زهري/أخضر فاتح Teal/أزرق بحري Ultramarine

إعلان الهاتف

آيفون 17 برو ماكس

آيفون 17 برو ماكس

يُعد هاتف iPhone 17 Pro Max قمة ما توفره أبل في عالم الهواتف الذكية، حيث يجمع بين التصميم العصري الجذاب بلونه البرتقالي الكوني والأداء الفائق بفضل معالج A19 Pro. ويقدم الهاتف تجربة بصرية مذهلة عبر شاشة Super Retina XDR العملاقة بحجم 6.9 بوصة، مدعومة بمنظومة تصوير متطورة تلبي طموحات عشاق المحتوى الاحترافي.

ملخص آراء أشهر المواقع التقنية

"«الشاشة الكبيرة والساطعة بحجم 6.9 بوصة تجعل مشاهدة المحتويات والألعاب تجربة غامرة بحق.»"

TrustedReviews
★★★★☆

"«نظام الكاميرات الثلاثي بدقة 48 ميجابكسل يقدم تفاصيل مذهلة وثباتاً استثنائياً في تصوير الفيديو الاحترافي.»"

TechRadar
★★★★☆

"«البطارية القوية مع معالجة الحرارة المحسنة تضمن أداءً مستداماً وطويلاً طوال اليوم دون أي تباطؤ.»"

The Verge
★★★★☆

المميزات

  • شاشة LTPO OLED فائقة السطوع بحجم 6.9 بوصة تدعم معدل تحديث 120 هرتز وتقنية Dolby Vision.
  • معالج Apple A19 Pro القوي المصمم بدقة 3 نانومتر لتقديم أداء سلس ومعالجة متقدمة لمهام الذكاء الاصطناعي.
  • منظومة كاميرات خلفية ثلاثية بدقة 48 ميجابكسل مع تقريب بصري محسن وكاميرا أمامية مبتكرة.
  • نظام تبريد متطور (غرفة بخار) يحافظ على استقرار الأداء وثباته خلال الاستخدام المكثف والألعاب.
  • عمر بطارية طويل ودعم للشحن السريع واللاسلكي المطور عبر تقنية MagSafe.

العيوب

  • الوزن والحجم الكبيران قد يجعلا استخدام الهاتف بـيد واحدة أمراً شاقاً وغير مريح لبعض المستخدمين لفترات طويلة.
  • ارتفاع السعر بشكل ملحوظ لكونه الإصدار الرائد الأعلى في عائلة أبل.
  • غياب مفردات التخزين القابلة للتوسعة (لا يدعم بطاقات الذاكرة الخارجية MicroSD).
  • سرعات الشحن السلكي واللاسلكي -رغم تحسنها- لا تزال تتخلف عن بعض الهواتف المنافسة في الفئة الرائدة.
  • خيارات التقريب البصري ما زالت تحتاج لتطوير أكبر لتنافس بعض القدرات الفائقة الموجودة لدى المنافسين.

مواصفات الهاتف

العنصرالمواصفات
المعالجشريحة A19 Pro وحدة معالجة مركزية مع 6 نوى
الكاميرا الخلفية48 ميجابكسل
سعة التخزين256 جيجابايت
حجم الشاشة6.9 بوصة
دقة الشاشة2868 × 1320
اسم الموديلiPhone 17 Pro Max
شبكة الاتصالمفتوح لجميع شبكات الاتصال
تقنية الاتصال الخلوي5G
تقنيات الاتصالBluetooth، NFC، USB، Wi-Fi
اللونبرتقالي كوني (Cosmic Orange)
Shopping Cart
اخترنا لك no posts found
×