-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinclude_ip2c.php
121 lines (101 loc) · 2.68 KB
/
include_ip2c.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
class ip2country {
public $mysql_host='mysql873.umbler.com';
public $db_name='luckprize';
public $db_user='luckpriz';
public $db_pass='190290ff';
public $table_name='ip2c';
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;
if(!$this->con)
$this->mysql_con();
$sq="SELECT country_code,country_name FROM ".$this->table_name. " WHERE ". $this->ip_num." BETWEEN begin_ip_num AND end_ip_num";
$r=@mysql_query($sq,$this->con);
if(!$r)
return '';
$row=@mysql_fetch_assoc($r);
$this->close();
$this->country_name=$row['country_name'];
$this->country_code=$row['country_code'];
return $row['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);
}
public function mysql_con()
{
$this->con=@mysql_connect($this->mysql_host,$this->db_user,$this->db_pass);
if(!$this->con)
return false;
if( !mysql_query('USE ' . $this->db_name))
{
$this->close();
return false;
}
return true;
}
public function get_mysql_con()
{
return $this->con;
}
public function create_mysql_table()
{
if(!$this->con)
return false;
mysql_query('DROP table ' . $this->table_name,$this->con);
return mysql_query("CREATE table " . $this->table_name ." (id int(10) unsigned auto_increment, begin_ip varchar(20),end_ip varchar(20),begin_ip_num int(11) unsigned,end_ip_num int(11) unsigned,country_code varchar(3),country_name varchar(150), PRIMARY KEY(id),INDEX(begin_ip_num,end_ip_num))ENGINE=MyISAM",$this->con);
}
public function close()
{
@mysql_close($this->con);
$this->con=false;
}
}
?>