How to get ip address using php code?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i want to show you How to get ip address using php code? Whenever we need to get the current user ip address then we can use the bellow example. I hope it will help you. We can find the client ip address by using $_SERVER variable in php.

Now you can see in the bellow example how to find the user ip address.

Example:
<?php
    function getClientIp() {
        $ipAddress = '';
        if (isset($_SERVER['HTTP_CLIENT_IP'])){
            $ipAddress = $_SERVER['HTTP_CLIENT_IP'];
        } else if(isset($_SERVER['REMOTE_ADDR'])){
            $ipAddress = $_SERVER['REMOTE_ADDR'];
        }else{
            $ipAddress = 'UnKnown';
        }
        return $ipAddress;
    }
	
    echo getClientIp();
?>

Read Also: Generate Captcha code and Validation by using BotDetect package in laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →