/
what-is-my-ip.php
62 lines (55 loc) · 1.24 KB
/
what-is-my-ip.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
<link type="text/css" rel="stylesheet" href="css/style.css">
<!-- //returns first forwarded IP match it finds-->
<?php
function forwarded_ip() {
$keys = array(
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'HTTP_CLIENT_IP',
'HTTP_X_CLUSTER_CLIENT-IP',
);
foreach($keys as $key) {
if(isset($_SERVER[$key])) {
$ip_array = explode(',', $_SERVER[$key]);
foreach($ip_array as $ip) {
$ip = trim($ip);
if(validate_ip($ip)) {
return $ip;
}
}
}
}
return '';
}
function validate_ip($ip) {
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
return false;
} else {
return true;
}
}
$remote_ip = $_SERVER['REMOTE_ADDR'];
$forwarded_ip = forwarded_ip();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>What is my IP</title>
</head>
<body>
<div id="main-content">
<h1>What Is My IP?</h1>
<h3>The request came from:</h3>
<h2><?php echo($_SERVER['REMOTE_ADDR']); ?></h2>
<br/>
<br/>
<?php if($forwarded_ip != '') {?>
<h3> The request was forwarded for:</h3>
<h2><?php echo $forwarded_ip; ?></h2>
<?php } ?>
</div>
</body>
</html>