Well, I had the same problem a while ago, but I managed to put together an API solution with some of my own PHP solutions, nothing extraordinary, after all, it's the API that does everything practically.
I will use generic names for the file naming, but I did it with the help of the GEOIP API, so I just created the PHP and MySQL part, which I hope will be useful for you.
Before, you need to register to receive your token via email: link
After this, just change the lines of the files correctly.
index.php
<script language="javascript">
var LIP_LowPrecision = false; //false = ask permission to the browser, higher precision | true = don't ask permission, lower precision
function LocalizaIP_done(ip_data){
if (!ip_data['error']) //this line is an exemple, you must change it by your Geolocation manipulation code
var pais = ip_data["countryCode"];
$.ajax({
data: 'pais=' + pais,
url: 'processa.php',
method: 'POST', // or GET
success: function(msg) {
//alert(msg);
if(msg == 'banido'){
window.location="http://meusite.com.br/404/";
}
}
});
}
</script>
<script src="https://www.localizaip.com/api/geolocation.js.php?domain=meusite.com.br&token=MEU_TOKEN=="></script>
process.php
<?php$hostname_conexao="localhost";
$username_conexao = "root";
$password_conexao = "";
$database_conexao = "teste";
$mysqli = new mysqli($hostname_conexao, $username_conexao, $password_conexao, $database_conexao);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$pais = $_POST['pais'];
$query = "SELECT pais FROM banirips WHERE pais='".$pais."'";
if($stmt_count = $mysqli->query($query))
{
$count_results = $stmt_count->num_rows;
$stmt_count->close();
}
if($count_results >= 1){echo "banido";}
?>
banirips.sql
id | pais |
1 | AR |
2 | BR |
You should only save the code of the desired country in your database. The system will compare what is saved and will block access. Just run a "BR" test on your database. Insert in the DB and do something like this after pulling the data:
Let's say that the variable "country"
if($pais == 'BR')
{
echo "PAÍS BLOQUEADO!";
}
else
{
echo "PAÍS SEM RESTRIÇÃO DE ACESSO!";
}
Is it something you want?