-
-
- Submit Updated Articles to Bing and Yandex with IndexNow in Joomla
- Add Published and Modified Date Meta Tags to Joomla
- Add Open Graph and Twitter Card Tags to Joomla
- Auto-Generate Meta Descriptions from Article Text in Joomla
- Add Noindex to Specific Pages in Joomla
- Verify Your Joomla Site with Google Search Console
-
- Add a View as Markdown Link to Joomla Articles
- Display the Last Modified Date in Joomla Articles
- Embed Iframes in Joomla
- Automatically Link Twitter (X) @mentions in Joomla Articles
- Open External Links in a New Tab in Joomla
- Display Estimated Reading Time in Joomla Articles
- Replace Old URLs in Joomla
- Search and Replace in Joomla
-
- Add a Footer Line to the Joomla Administrator
- Show a Staging Banner in the Joomla Administrator
- Remove the "All" Option from Admin List Limits in Joomla
- Add a Cookie Consent Banner to Joomla
- Make the Administrator Navigation Sticky in Joomla
- Add Google Fonts to Joomla
- Add a Facebook Messenger Chat Button to Joomla
- Add a Reading Progress Bar to Joomla
- Add a Custom Scrollbar to Joomla
- Show Confetti on a Thank You Page in Joomla
- Add Smooth Scrolling to Joomla
- Add a Back to Top Button in Joomla
- Add a WhatsApp Chat Button in Joomla
-
- Protect the Administrator with a Secret URL Key in Joomla
- Show a Welcome Message on First Login in Joomla
- Log Out Idle Administrators in Joomla
- Assign New Users to a Different User Group Automatically in Joomla
- Redirect Visitors by Country in Joomla
- Redirect Users After Login by User Group in Joomla
- Block User Registration with Certain Usernames in Joomla
- Block User Registration by Country in Joomla
- Block User Registration by Email Address in Joomla
- Block User Registration by IP Address in Joomla
- Remove the Forgot Password and Username Links in Joomla
- Remove the Generator Meta Tag in Joomla
- Let Users Log In with Their Email Address in Joomla
- Block Traffic by Country in Joomla
- Block Bot Traffic in Joomla
- Block Traffic by IP Address in Joomla
Block User Registration by Country in Joomla
Stop fake sign-ups from countries you never serve, or open Joomla registration only to the countries you do, using one PHP snippet.
Fake accounts arrive every night from 2 countries you've never sold to, and the senders change IP addresses faster than you can block them. You want the site to stay open to everyone, but registration only for the countries you serve. You can block user registration by country in Joomla with one PHP snippet in Code Snippets.
The snippet finds each visitor's country with TGeoIP, the free geolocation plugin that installs together with Code Snippets. The lookup runs on your own server from a local database, so visitor addresses never go to an outside service.
Before you start
TGeoIP needs the free MaxMind GeoLite2 database to tell where an address comes from. If you haven't set it up yet, enable geolocation first: add a free MaxMind license key to the System - Tassos GeoIP plugin and click Update Database.
The PHP snippet code
The snippet runs right before Joomla stores a new account. It looks up the visitor's country and, depending on the mode, cancels the registration and shows your message on the form. Everything else on the site keeps working for that visitor, and existing accounts can still log in.
use Joomla\CMS\Factory;
use Tassos\Framework\Helpers\Geo;
// Two-letter country codes, for example 'CN' for China or 'GR' for Greece.
$countries = [
'CN',
'RU',
];
// 'block' rejects sign-ups from the countries above.
// 'allow' accepts sign-ups only from the countries above.
$mode = 'block';
// Message shown on the registration form instead of the welcome message.
$message = 'Registration is not available in your country.';
// Do not edit below.
// No Tassos framework means no country lookup, so let everyone register.
if (!class_exists(Geo::class))
{
return;
}
$app = Factory::getApplication();
$app->registerEvent('onUserBeforeSave', function ($event) use ($countries, $mode, $message)
{
// Only new accounts. Profile edits pass through.
if (!$event->getArgument('isNew', $event->getArgument(1, false)))
{
return;
}
$country = strtoupper((string) Geo::getVisitorCountryCode());
// Unknown country, for example a local address or a missing GeoIP database.
// Let the visitor register rather than lock everyone out.
if ($country === '')
{
return;
}
$countries = array_map('strtoupper', array_map('trim', $countries));
$listed = in_array($country, $countries, true);
$blocked = $mode === 'allow' ? !$listed : $listed;
if ($blocked)
{
// Joomla cancels the registration and shows this message.
throw new RuntimeException($message);
}
});
Change the 3 settings at the top:
$countries: 2-letter country codes, for exampleCNfor China,RUfor Russia orGRfor Greece. You'll find every code in the ISO 3166-1 alpha-2 list. Lowercase works too.$mode: keep'block'to reject sign-ups from the countries on your list. Set it to'allow'to accept sign-ups only from those countries and reject everyone else.$message: the text Joomla shows on the registration form. It appears after "Registration failed:".
When the snippet can't tell where a visitor comes from, for example because the GeoIP database is missing, it lets the registration through. A broken lookup never locks out your real visitors.
How to add this snippet
- Install Code Snippets if it isn't installed already.
- Log into your Joomla backend.
- Go to Components → Tassos Code Snippets.
- Click New.
- Enter a title, for example Block registration by country.
- Set Code Type to PHP.
- Set Insertion Method to Auto-Insert Locations.
- Set Location to On Page Load.
- Paste the code above into the editor.
- Replace the sample country codes with the ones you want to block.
- Click Save & Close.
Check that it works
- Add your own country code to
$countriesand save the snippet. - Open your site's registration page in a private browser window and submit the form with a test name and email.
Joomla shows "Registration failed: Registration is not available in your country." and creates no account. Remove your country from the list, save, and the same form goes through.
If sign-ups from a blocked country still get through
- Your site sits behind Cloudflare or another proxy. Joomla may then see the proxy's address instead of the visitor's, so the snippet checks the wrong country. Go to System → Global Configuration, open the Server tab, set Behind Load Balancer to Yes, and click Save & Close. Leave it off when there's no proxy, because it lets visitors fake their address.
- The GeoIP database is missing. The snippet then lets everyone register. Open the System - Tassos GeoIP plugin and click Update Database.
- The visitor uses a VPN. The snippet sees the VPN's address, so the visitor appears to come from the VPN's country.
To keep a country off the whole site rather than just the registration form, see Block Traffic by Country in Joomla. To block sign-ups by IP address, email address or username, see Block User Registration by IP Address in Joomla, Block User Registration by Email Address in Joomla and Block User Registration with Certain Usernames in Joomla. To learn more about how PHP snippets work, read the PHP snippet docs.