How to get Real IP from Visitor

0 votes

I'm using this PHP code to get a visitor's IP address:

<?php echo $_SERVER['REMOTE_ADDR']; ?>

But, I can't get the real IP address from visitors when they are using a proxy. Is there any way to get a visitor's IP address in this case?

Oct 6, 2020 in PHP by kartik
• 37,510 points
1,688 views

1 answer to this question.

0 votes

Hello @kartik,

Try this php code.

<?PHP

function getUserIP()
{
    // Get real visitor IP behind CloudFlare network
    if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
              $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
              $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
    }
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}


$user_ip = getUserIP();

echo $user_ip; // Output IP address [Ex: 177.87.193.134]

Hope it helps!!
Thank you!!

answered Oct 6, 2020 by Niroj
• 82,880 points

Related Questions In PHP

0 votes
1 answer

How to get the client IP address in PHP ?

Hello, Here is a code sample of a good ...READ MORE

answered Apr 8, 2020 in PHP by Niroj
• 82,880 points
6,513 views
0 votes
1 answer

How to get parameters from a URL string?

Hello @kartik, You can use the parse_url() and parse_str() for that. $parts = ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,880 points
624 views
0 votes
1 answer

How to Get $_POST from multiple checkboxes?

Hello @kartik, Set the name in the form ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,880 points
3,213 views
0 votes
1 answer

How to get the home directory from a PHP CLI script?

Hello @kartik, You can fetch the value of ...READ MORE

answered Nov 3, 2020 in PHP by Niroj
• 82,880 points
3,383 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,875 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,678 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,541 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,716 views
0 votes
1 answer

How to get the location from an IP address?

Hello @kartik, You could download a free GeoIP ...READ MORE

answered Sep 17, 2020 in PHP by Niroj
• 82,880 points
2,102 views
0 votes
1 answer

How to get thumbnail of youtube video from youtube API?

Hii @kartik, If you want the biggest image ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,880 points
1,431 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP