Created
November 15, 2024 16:12
-
-
Save gerard-kanters/2270446f560ba012901bda5d12c7ea35 to your computer and use it in GitHub Desktop.
Use GEOIP in Wordpress to protect contact form.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//GeoIP gebruiken om contact pagina te beschermen tegen spammers | |
// Dynamisch pad bepalen voor Composer autoload | |
$autoload_path = ABSPATH . 'vendor/autoload.php'; | |
if (!file_exists($autoload_path)) { | |
error_log('Composer autoload.php niet gevonden. Controleer of Composer correct is geïnstalleerd.'); | |
return; // Stop het script als Composer niet beschikbaar is | |
} | |
require_once $autoload_path; | |
use GeoIp2\Database\Reader as GeoIpReader; | |
function restrict_access_with_php_redirect() { | |
$user_ip = $_SERVER['REMOTE_ADDR']; | |
$reader = new GeoIpReader('/usr/share/geoip/GeoLite2-Country.mmdb'); | |
try { | |
// Haal landcode en -naam op voor de bezoeker | |
$record = $reader->country($user_ip); | |
$country_code = $record->country->isoCode; | |
$country_name = $record->country->name; | |
// Log de landcode voor debuggen | |
error_log("Landcode: " . $country_code); | |
// Controleer toegang op basis van pagina | |
if (is_page('contact')) { | |
$allowed_countries = ['NL', 'BE']; // Toegestane landen voor /contact | |
if (!in_array($country_code, $allowed_countries)) { | |
show_overlay_and_redirect("Access to this page is not allowed from $country_name."); | |
} | |
} elseif (is_page('contact-us')) { | |
$allowed_countries = ['US', 'GB', 'CA', 'AU', 'NL', 'BE']; // Toegestane landen voor /contact-us | |
if (!in_array($country_code, $allowed_countries)) { | |
show_overlay_and_redirect("Access to this page is not allowed from $country_name."); | |
} | |
} | |
} catch (Exception $e) { | |
error_log("GeoIP-locatiefout: " . $e->getMessage()); | |
wp_die('Er is een probleem opgetreden bij het controleren van je locatie.'); | |
} | |
} | |
// Functie om overlay en redirect te genereren met PHP | |
function show_overlay_and_redirect($message) { | |
// HTML en CSS voor de overlay | |
echo " | |
<style> | |
#access-denied-overlay { | |
position: fixed; | |
top: 20%; | |
left: 50%; | |
transform: translate(-50%, -20%); | |
width: 300px; | |
background-color: rgba(0, 0, 0, 0.9); | |
color: white; | |
z-index: 9999; | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
justify-content: center; | |
font-size: 14px; | |
text-align: center; | |
border-radius: 8px; | |
padding: 20px; | |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); | |
opacity: 0.95; | |
} | |
#access-denied-overlay p { | |
margin: 10px 0; | |
} | |
</style> | |
<div id='access-denied-overlay'> | |
<p>$message</p> | |
<p>You will be redirected to the homepage in 6 seconds.</p> | |
</div> | |
"; | |
// Header-redirect na 6 seconden | |
header("Refresh:6; url=" . home_url()); | |
} | |
add_action('template_redirect', 'restrict_access_with_php_redirect'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires geoip database installed "/usr/share/geoip/GeoLite2-Country.mmdb"
run "composer require geoip2/geoip2" from the home directory of your project