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*/
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?>
1$ip = $_SERVER['REMOTE_ADDR'];
2$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
3echo $details->city; // -> "Mountain View"