1<?PHP
2
3function getUserIP()
4{
5 // Get real visitor IP behind CloudFlare network
6 if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
7 $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
8 $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
9 }
10 $client = @$_SERVER['HTTP_CLIENT_IP'];
11 $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
12 $remote = $_SERVER['REMOTE_ADDR'];
13
14 if(filter_var($client, FILTER_VALIDATE_IP))
15 {
16 $ip = $client;
17 }
18 elseif(filter_var($forward, FILTER_VALIDATE_IP))
19 {
20 $ip = $forward;
21 }
22 else
23 {
24 $ip = $remote;
25 }
26
27 return $ip;
28}
29
30
31$user_ip = getUserIP();
32
33echo $user_ip; // Output IP address [Ex: 177.87.193.134]
34
35
36?>