Well, I had the same problem a while ago, but I managed to join the solution of an API with some own solutions in PHP, nothing extraordinary, after all, it is the API that does everything practically.
I will use generic names for file appointments, but I managed with the help of the GEOIP API, then just create the PHP and Mysql part, which I hope will be useful for you.
before, you need to register to receive your token via email: https://www.localizaip.com.br/api_localiza_ip.php
After this, just change the file lines 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>
parses.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 must save only the code of the desired country in your database. The system will compare what is saved and will block access. Just do a test with "BR" in your database. Insert it in the BD and do something like that after pulling the data:
Let’s say the variable "country"
if($pais == 'BR')
{
echo "PAÍS BLOQUEADO!";
}
else
{
echo "PAÍS SEM RESTRIÇÃO DE ACESSO!";
}
It’s something you want?
Have geoip API. Read about?
– Alex Lupóz
I’ll read it. Thanks Alex.
– Eduardo
i decided to respond with a project I have here. I hope it will be useful somehow.
– Alex Lupóz