ip to country * * More info can be found at http://phpweby.com/link * ************************************************************************** License: This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *********************************************************/ class ip2country { //var $mssql_host='CT8354\SQLEXPRESS'; var $mssql_host='27.112.104.147, 1433'; var $db_name='iTailor'; var $db_user='sa'; var $db_pass='SpeedAGI^14'; var $table_name='IP2COUNTRY'; var $ip_num=0; var $ip=''; var $country_code=''; var $country_name=''; var $con=false; function ip2country() { $this->set_ip(); } function get_ip_num() { return $this->ip_num; } function set_ip($newip='') { if($newip=='') $newip=$this->get_client_ip(); $this->ip=$newip; $this->calculate_ip_num(); $this->country_code=''; $this->country_name=''; } function calculate_ip_num() { if($this->ip=='') $this->ip=$this->get_client_ip(); $this->ip_num=sprintf("%u",ip2long($this->ip)); } 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; if(!$this->con) $this->mssql_con(); $sq="SELECT country_code2 FROM ".$this->table_name. " WHERE ". $this->ip_num." BETWEEN ip_from AND ip_to"; //echo $sq; $r=@odbc_exec($this->con,$sq); if(!$r) return ''; $row=@odbc_fetch_array($r); $this->close(); //$this->country_name=$row['country_name']; $this->country_code=$row['country_code2']; return $row['country_code2']; } /* function get_country_name($ip_addr='') { $this->get_country_code($ip_addr); return $this->country_name; } */ 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); } function mssql_con() { $this->con=@odbc_connect("Driver={SQL Server};Server=".$this->mssql_host.";Database=".$this->db_name.";",$this->db_user,$this->db_pass); if(!$this->con){ echo "Fail to connect DB\n"; return false; } return true; } function get_mssql_con() { return $this->con; } function close() { @odbc_close($this->con); $this->con=false; } } ?>