Categories
PHP

How to get visitors IP address in PHP

Find out the IP address of visitor is very easy process in PHP. Finding the IP address is important requirement for many application where we want store/save the details of client/visitor. For many security reason we need to find out the IP of client/visitor such as online purchases,  online transaction etc. We can find out the GEO Location of client/visitor by using the IP address. So finding the very important. Let’s write some code for finding the IP address.

<?php
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    echo $ip;
?>

You can simply copy this code and paste into your script.

NB: It is working properly in proxy IP.

 

By Sohel Rana

PHP Programmer, Software Engineer & Technology lover

Leave a Reply

Your email address will not be published. Required fields are marked *