How to get the IP address in PHP
×


How to get the IP address in PHP

1556

When user visit on website, so we can easily get the IP address and store into database. Through this IP address we can use for multi-purpose. We can block IP address and can track user ISP easily with location.

Example:

Your IP address - ::1

Source Code:

<?php
echo 'User IP Address - '.$_SERVER['REMOTE_ADDR'];
?>

Result:

User IP Address - ::1

::1 this is local server IP address, that we use into XAMPP, WAMPP.

Sometime REMOTE_ADDR not returned the IP address, so for that purpose we use different method. It happens when user use proxy or security for hide the IP.

It’s mostly uses by hackers to hack the website.

Now we use different method to get the IP address.

Source Code:

<?php
    function find_ip_id() {
    // whether IP is from the share internet
     if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
             $user_id = $_SERVER['HTTP_CLIENT_IP'];
        }
    // whether ip is from the proxy
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
           $user_id = $_SERVER['HTTP_X_FORWARDED_FOR'];
     }
// whether ip is from the remote address
    else{
          $user_id = $_SERVER['REMOTE_ADDR'];
     }
    return $user_id;
}
$user_ip_id = find_ip_id();
echo 'User Real IP Address - ' . $user_ip_id;
?>

Result:

User Real IP Address - ::1



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments