topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 8:43 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: GDPR Blocking (PHP Edition)  (Read 37608 times)

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,466
    • View Profile
    • Donate to Member
GDPR Blocking (PHP Edition)
« on: May 04, 2018, 05:27 AM »
There is an expensive web service that makes your website GDPR-compliant by adding a JavaScript that redirects visitors from the EU to an error page.

This is my attempt to provide a simple and free PHP script for those who want to achieve the same result on server side.

Code: PHP [Select]
  1. <?php
  2. /*
  3.   Copyright © 2018 tux. <[email protected]>
  4.   This work is free. You can redistribute it and/or modify it under the
  5.   terms of the Do What The Fuck You Want To Public License, Version 2,
  6.   as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
  7.  
  8.   GDPR SHIELD [PHP draft v1]
  9.  
  10.   Prerequisites:
  11.   * PHP
  12.   * GeoIP as described here: http://php.net/manual/en/geoip.requirements.php
  13.  
  14.   Usage:
  15.   <?php require("gdprshield.php"); ?>
  16.   <!doctype html>
  17.   <html>
  18.     ...
  19.   </html>
  20. */
  21. $disallowed_countries = ["BE", "BG", "CZ", "DK", "DE", "EE", "IE", "EL", "ES", "FR", "HR", "IT", "CY", "LV", "LT", "LU", "HU", "MT", "NL", "AT", "PL", "PT", "RO", "SI", "SK", "FI", "SE", "UK"];
  22. $ip = $_SERVER["REMOTE_ADDR"];
  23. if (in_array(geoip_country_code_by_name($ip), $disallowed_countries)) {
  24.     die("Your country does not want you to be here.");
  25.  
  26.     /*
  27.       The above code displays a plain error text.
  28.       If you prefer to redirect to a full-featured HTML page,
  29.       you could delete it and use a Location header instead:
  30.     */
  31.     header("Location: GDPR_blocked.php", true, 451);
  32. }
  33. ?>

Enjoy. And feel free to extend/fix/implement it as you wish.
« Last Edit: May 04, 2018, 09:53 AM by Tuxman, Reason: enhanced redirection »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #1 on: May 04, 2018, 05:47 AM »
Tux, just wanted to say I appreciate you sharing your projects with us.  :up:

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,466
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #2 on: May 04, 2018, 05:48 AM »
That sounds like "P.S.: You're banned forever."  ;D

I thought this is a developer site where developers share their projects, so I do that. (Although this is more like an unrequested coding snack...)
Anyway, you're welcome.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #3 on: May 04, 2018, 06:09 AM »
There is no ps, it was a genuine expression of thanks.

Like many aspects of life, sometimes it takes perseverance and stubbornness and just charting your own path and doing things without much feedback until the people who appreciate your work find you.

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,466
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #4 on: May 04, 2018, 06:15 AM »
Ah, the day on which I lose interest in writing code just to keep boredom off me will be a sad day for me.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #5 on: May 04, 2018, 09:41 AM »
I've actually been puzzling over the ramifications of this, so this seems like an alternative to the people that try to make money off of a bad mistake.

Thanks!

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,747
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #6 on: May 15, 2018, 12:34 PM »
Found a reference to this on Twitter:

https://twitter.com/...s/992370970928140288

Also, it seems to me that the GDPR Shield website is currently down. I wonder why... :-\

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,466
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #7 on: May 15, 2018, 12:38 PM »
Too many customers?  ;D

The Code Queryer

  • Participant
  • Joined in 2019
  • *
  • default avatar
  • Posts: 33
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #8 on: April 05, 2019, 02:08 PM »
How come this ain't working for me ?
I get error:

Fatal error: Uncaught Error: Call to undefined function geoip_country_code_by_name() in C:\xampp\htdocs\test\visitor_country.php:23 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test\visitor_country.php on line 23
« Last Edit: April 25, 2019, 10:31 AM by The Code Queryer »

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,466
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #9 on: April 06, 2019, 09:49 AM »
"I'll write a README directly into the code so everyone knows what to do."
- Developers (inexperienced).

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #10 on: April 06, 2019, 11:10 AM »
Specifically, in case he was being too obtuse, this in the code.

Code: PHP [Select]
  1. Prerequisites:
  2.   * PHP
  3.   * GeoIP as described here: http://php.net/manual/en/geoip.requirements.php


Though I wouldn't disagree that a bit of obtuseness was perhaps warranted in this case.

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,466
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #11 on: April 06, 2019, 11:13 AM »
In fact, the PHP manual is more descriptive than I could be, especially with its comments section.

The Code Queryer

  • Participant
  • Joined in 2019
  • *
  • default avatar
  • Posts: 33
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #12 on: April 06, 2019, 12:23 PM »
There is an expensive web service that makes your website GDPR-compliant by adding a JavaScript that redirects visitors from the EU to an error page.

This is my attempt to provide a simple and free PHP script for those who want to achieve the same result on server side.

Code: PHP [Select]
  1. <?php
  2. /*
  3.   Copyright © 2018 tux. <[email protected]>
  4.   This work is free. You can redistribute it and/or modify it under the
  5.   terms of the Do What The Fuck You Want To Public License, Version 2,
  6.   as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
  7.  
  8.   GDPR SHIELD [PHP draft v1]
  9.  
  10.   Prerequisites:
  11.   * PHP
  12.   * GeoIP as described here: http://php.net/manual/en/geoip.requirements.php
  13.  
  14.   Usage:
  15.   <?php require("gdprshield.php"); ?>
  16.   <!doctype html>
  17.   <html>
  18.     ...
  19.   </html>
  20. */
  21. $disallowed_countries = ["BE", "BG", "CZ", "DK", "DE", "EE", "IE", "EL", "ES", "FR", "HR", "IT", "CY", "LV", "LT", "LU", "HU", "MT", "NL", "AT", "PL", "PT", "RO", "SI", "SK", "FI", "SE", "UK"];
  22. $ip = $_SERVER["REMOTE_ADDR"];
  23. if (in_array(geoip_country_code_by_name($ip), $disallowed_countries)) {
  24.     die("Your country does not want you to be here.");
  25.  
  26.     /*
  27.       The above code displays a plain error text.
  28.       If you prefer to redirect to a full-featured HTML page,
  29.       you could delete it and use a Location header instead:
  30.     */
  31.     header("Location: GDPR_blocked.php", true, 451);
  32. }
  33. ?>

Enjoy. And feel free to extend/fix/implement it as you wish.

According to this link:
https://www.php.net/...oip.requirements.php

You need c library installed. Not sure how to install that on my cPanel. I was looking for something with php alone.

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,922
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #13 on: April 06, 2019, 06:13 PM »
GeoLite2 is what you likely could use.

You could create a subdomain using cPanel and divert users from the GDPR countries to that subdomain where you make a simple page that explains why they are not allowed access. That gives you still an overview of how many visitors from GDPR countries wanted to visit your site and these visitors have an inkling of why access was denied and (hopefully) create so much outcry that politicians gladly take action to cut GDPR short before the internet becomes (too) divided. 

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,466
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #14 on: April 06, 2019, 06:15 PM »
(hopefully) create so much outcry that politicians gladly take action to cut GDPR short before the internet becomes (too) divided. 

HAHAHAHAHAHAHAHA
HAHAHAHA

I mean

HAHAHAHAHAHAHAHA omg *snort* AHAHAHAHAHAHAHHAHAHAHAHA

... wait, you were serious?

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,922
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #15 on: April 06, 2019, 06:39 PM »
If I would not have added "(hopefully)" then yes, it would be serious.

But rest assured, I am very familiar with the unbridled apathy and lazyness of the general public at large and millennials in particular. Hence the addition.

The Code Queryer

  • Participant
  • Joined in 2019
  • *
  • default avatar
  • Posts: 33
    • View Profile
    • Donate to Member
Re: GDPR Blocking (PHP Edition)
« Reply #16 on: April 25, 2019, 10:28 AM »
At the end, few days ago, I bought a gig at fiver that redirects outside my site all EU visitors through the apache module geoip.