1

I want to get IP Address of my local system which i am using. on my website if am using 'HTTP_CLIENT_IP' than it returns IP Address like 182.74 etc but i want to get current computer's IP Adress on which system my website is running IP Address like "192.168" . I am using this code :-

function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
} 

How can i get this IP address on my website.

Ranjeet singh
  • 794
  • 1
  • 16
  • 38
  • 2
    so you want to get the *private* IP-address instead of the *public* address? the one that isn't routable by definition and, basically, does not *exist* outside of your LAN? the one that gets replaced by your router with the public one in every packet leaving your network? – Franz Gleichmann Nov 18 '16 at 06:45
  • possible duplicate of http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php?rq=1 – Naga Nov 18 '16 at 06:46
  • 2
    >>>>> 127.0.0.1 – keyboardSmasher Nov 18 '16 at 06:54
  • @NAGA its not working for me . its again return IP like "182.74". – Ranjeet singh Nov 18 '16 at 07:10
  • @Franz , I want to get that ip address of local computer on which my website is running . if its open on your computer than it show your local system IP and if on mine than mine local system IP . my local system IP like " 192.168.etc " . – Ranjeet singh Nov 18 '16 at 07:13
  • 1
    @Ranjeetsingh "on which your website is running".. the website is *running* on your **server**. that address can be read in `$_SERVER['SERVER_ADDR'];`. but the local address of your computer, in front of which you are sitting? - please *read* my first comment. that "local system IP", which is called "private IP", gets obliterated the moment it leaves your network. it's **impossible** for the server to get it. you may want to read up a few tutorials about "Private IP-Adresses" and "network address translation (NAT)" – Franz Gleichmann Nov 18 '16 at 07:16
  • Use the operating system's command to output the machine local/private IP, with PHP's exec() or shell_exec() (depending on what suits your needs). Then in PHP you need to process the result of the function call to extract the IP from the result. But before all this, ask yourself why and if you really need this. Revealing machine details like that is considered a security issue in general... as is using exec or shell_exec btw... – Werner Nov 18 '16 at 08:07
  • Or, from the operating system (batch file ipconfig > blabla or shell script or whatever), just generate a flat file/txt-file from the Operating System that contains the IP-address. Then from your PHP code, you open the file, read it and fetch the IP-address... this is safer than executing code on your OS as commented above by me... good luck. – Werner Nov 18 '16 at 08:12

0 Answers0