Current File : /home/tradevaly/new.tradevaly.com.bd/app/Traits/Ipcountry.php |
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\Ip;
trait Country {
private $ip_num=0;
private $ip='';
private $country_code='';
private $country_name='';
private $con=false;
function ip2country()
{
$this->set_ip();
}
public function get_ip_num()
{
return $this->ip_num;
}
public function set_ip($newip='')
{
if($newip=='')
$newip=$this->get_client_ip();
$this->ip=$newip;
$this->calculate_ip_num();
$this->country_code='';
$this->country_name='';
}
public function calculate_ip_num()
{
if($this->ip=='')
$this->ip=$this->get_client_ip();
$this->ip_num=sprintf("%u",ip2long($this->ip));
}
public function get_country_code($ip_addr='')
{
if($ip_addr!='' && $ip_addr!=$this->ip)
$this->set_ip($ip_addr);
if($ip_addr=='')
{
if($this->ip!=$this->get_client_ip())
$this->set_ip();
}
if($this->country_code!='')
return $this->country_code;
$country = Ip::where('begin_ip_num', '<=', $this->ip_num)
->where('end_ip_num', '>=', $this->ip_num)->first();
if(!$country)
return '';
$this->country_name=$country->country_name;
$this->country_code=$country->country_code;
return $country->country_code;
}
public function get_country_name($ip_addr='')
{
$this->get_country_code($ip_addr);
return $this->country_name;
}
public function get_client_ip()
{
$v='';
$v= (!empty($_SERVER['REMOTE_ADDR']))?$_SERVER['REMOTE_ADDR'] :((!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR']: @getenv('REMOTE_ADDR'));
if(isset($_SERVER['HTTP_CLIENT_IP']))
$v=$_SERVER['HTTP_CLIENT_IP'];
return htmlspecialchars($v,ENT_QUOTES);
}
}