Skip to content

Commit 619d2bd

Browse files
committed
dev-stable & composer ready
1 parent 836a06f commit 619d2bd

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "dimchtz/clientip",
3+
"description": "Client IP detector and various IP validators",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Dimitris Chatzis",
9+
"email": "dim.chatzis1993@gmail.com"
10+
}
11+
],
12+
"require": {},
13+
"autoload": {
14+
"psr-4": {
15+
"DimChtz\\ClientIP\\": "src/"
16+
}
17+
}
18+
}

src/clientip.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@
1919
class ClientIP {
2020

2121
/**
22-
* Ip find services
22+
* IP Services
2323
* @var string
2424
*/
2525
private $services;
2626

2727
public function __construct($services = array()) {
2828

2929
$this->services = array_merge(array(
30-
'http://checkip.dyndns.com/',
30+
'http://v4.ident.me/',
31+
'http://checkip.amazonaws.com/',
3132
'http://ipecho.net/plain',
32-
'http://v4.ident.me/'
33+
'http://icanhazip.com/',
34+
'http://ifconfig.me/ip',
35+
'http://checkip.dyndns.com/',
3336
), $services);
3437

35-
$this->timeout = $timeout;
36-
3738
}
3839

3940
/**
@@ -44,26 +45,25 @@ public function get_external_ip() {
4445

4546
$ip = '0.0.0.0';
4647

48+
// Some IP services may stop working or throttle our requests
4749
foreach ( $this->services as $service ) {
4850

4951
$ch = curl_init($service);
5052

51-
$headers = array(
52-
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
53-
"Accept-Encoding: gzip, deflate",
54-
"Accept-Charset: utf-8;q=0.7,*;q=0.3",
55-
"Accept-Language:en-US;q=0.6,en;q=0.4",
56-
"Connection: keep-alive",
57-
);
58-
5953
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
6054
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
6155
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
6256
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
6357
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
64-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
58+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
59+
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
60+
"Accept-Encoding: gzip, deflate",
61+
"Accept-Charset: utf-8;q=0.7,*;q=0.3",
62+
"Accept-Language:en-US;q=0.6,en;q=0.4",
63+
"Connection: keep-alive",
64+
));
6565

66-
$result = curl_exec($ch);
66+
$result = trim(curl_exec($ch));
6767

6868
curl_close($ch);
6969

@@ -96,6 +96,7 @@ public function get_ip($localhost_check = true) {
9696
$ip = $_SERVER['REMOTE_ADDR'];
9797
}
9898

99+
// If localhost and localhost check is enabled return the external IP instead
99100
if ( in_array($ip, ['127.0.0.1', '::1']) && $localhost_check ) {
100101
return $this->get_external_ip();
101102
}

0 commit comments

Comments
 (0)