File tree 2 files changed +66
-0
lines changed
2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,51 @@ public static function isValidv6($ip)
73
73
return (bool )filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 );
74
74
}
75
75
76
+ /**
77
+ * Checks if an IP is local
78
+ *
79
+ * @param string $ip IP
80
+ * @return boolean true if the IP is local, otherwise false
81
+ */
82
+ public static function isLocal ($ ip )
83
+ {
84
+ $ localIpv4Ranges = array (
85
+ '10.*.*.* ' ,
86
+ '127.*.*.* ' ,
87
+ '192.168.*.* ' ,
88
+ '169.254.*.* ' ,
89
+ '172.16.0.0-172.31.255.255 ' ,
90
+ '224.*.*.* ' ,
91
+ );
92
+
93
+ $ localIpv6Ranges = array (
94
+ 'fe80::/10 ' ,
95
+ '::1/128 ' ,
96
+ 'fc00::/7 '
97
+ );
98
+
99
+ if (self ::isValidv4 ($ ip )) {
100
+ return self ::match ($ ip , $ localIpv4Ranges );
101
+ }
102
+
103
+ if (self ::isValidv6 ($ ip )) {
104
+ return self ::match ($ ip , $ localIpv6Ranges );
105
+ }
106
+
107
+ return false ;
108
+ }
109
+
110
+ /**
111
+ * Checks if an IP is remot
112
+ *
113
+ * @param string $ip IP
114
+ * @return boolean true if the IP is remote, otherwise false
115
+ */
116
+ public static function isRemote ($ ip )
117
+ {
118
+ return !self ::isLocal ($ ip );
119
+ }
120
+
76
121
/**
77
122
* Checks if an IP is part of an IP range.
78
123
*
Original file line number Diff line number Diff line change @@ -190,4 +190,25 @@ public function test9()
190
190
$ this ->assertEquals ('fe80::202:b3ff:fe1e:8329 ' , $ dec );
191
191
}
192
192
193
+ /**
194
+ * @test
195
+ */
196
+ public function testLocal ()
197
+ {
198
+ $ status = Ip::isLocal ('192.168.5.5 ' );
199
+ $ this ->assertTrue ($ status );
200
+
201
+ $ status = Ip::isLocal ('fe80::202:b3ff:fe1e:8329 ' );
202
+ $ this ->assertTrue ($ status );
203
+ }
204
+
205
+ /**
206
+ * @test
207
+ */
208
+ public function testRemote ()
209
+ {
210
+ $ status = Ip::isRemote ('8.8.8.8 ' );
211
+ $ this ->assertTrue ($ status );
212
+ }
213
+
193
214
}
You can’t perform that action at this time.
0 commit comments