1The simplest way to collect the Client/Visitor IP address using PHP is the REMOTE_ADDR.
2Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage.
3
4Get the IP address of the website
5<?php
6echo 'User IP Address : '. $_SERVER['REMOTE_ADDR'];
7?>
8
9/*
10I Hope it will help you.
11Namaste
12Stay Home Stay Safe
13*/
1function getIp() {
2 $ip = $_SERVER['REMOTE_ADDR'];
3
4 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
5 $ip = $_SERVER['HTTP_CLIENT_IP'];
6 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
7 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
8 }
9
10 return $ip;
11}
1#to best handle proxies use this:
2if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
3 $ip = $_SERVER['HTTP_CLIENT_IP'];
4} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
5 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
6} else {
7 $ip = $_SERVER['REMOTE_ADDR'];
8}
1<?php
2
3// Most effective way to get users IP
4function get_ip_address(){
5 foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
6 if (array_key_exists($key, $_SERVER) === true){
7 foreach (explode(',', $_SERVER[$key]) as $ip){
8 $ip = trim($ip); // just to be safe
9
10 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
11 return $ip;
12 }
13 }
14 }
15 }
16}
17
18?>