ahmetkarakoc Bir tane tablo oluşturun ve IP bloklarını tabloda tutun. MySQL kullandığınızı varsayıyorum:
class CreateIpsTable extends Migration
{
public function up()
{
Schema::create('ips', function (Blueprint $table) {
$table->id();
$table->ipAddress('start_ip');
$table->ipAddress('end_ip');
$table->polygon('ip_range');
});
DB::statement("CREATE SPATIAL INDEX ips_ip_range_index ON ips (ip_range);");
}
public function down()
{
Schema::drop('ips');
}
}
Bu tabloya kayıt eklerken şu şekilde ekleyeceksiniz:
$sql = <<<SQL
INSERT INTO
ips (start_ip, end_ip, ip_range)
VALUES (
?, ?, Polygon(LineString(
Point(?, 1),
Point(?, 0),
Point(?, 0),
Point(?, 1),
Point(?, 1)
)
)
)
SQL;
DB::insert($sql, [
$startIp,
$endIp,
$startIp,
$startIp,
$endIp,
$endIp,
$startIp
]);
Dosyadan gelen IP, IP blokları içinde mi kontrol etmek için de:
$sql = <<<SQL
INTERSECT (ip_range,
Polygon(LineString(
Point(?, 1),
Point(?, 0),
Point(?, 0),
Point(?, 1),
Point(?, 1)
))
)
SQL;
$inBlock = DB::table('ips')
->whereRaw($sql, [
$startIp,
$startIp,
$endIp,
$endIp,
$startIp
])
->exists();
Test etme fırsatım olmadı ama bu size fikir verebilir. Belki mevcut sisteminizden daha hızlı olur.
Şu paketi deneyebilirsiniz:
https://github.com/rlanvin/php-ip
use PhpIP\IPBlock;
$block = IPBlock::create('10.0.0.0/24');
$block->contains('10.0.0.42'); // true