简体   繁体   中英

How to get ip address of client connected to remote desktop

i need to retrieve the ip address of the client that is connected through remote desktop to a windows server.

i need to get it possibly through php.

So client connects to remote desktop and run from browser a php page that is on the server which client is connected.

If i run print_r($_SERVER) i get:

Array (
...
[HTTP_HOST] => 10.80.3.107 //This is server ip
...
[SERVER_NAME] => 10.80.3.107 //This is server ip
[SERVER_ADDR] => 10.80.3.107 //This is server ip
[SERVER_PORT] => 80
[REMOTE_ADDR] => 10.80.3.107 //This is server ip -> I need client ip here
...
)

Is there any solution?

can i use cmd to get that info and than take it from php using exec?

I can't use netstat -n | find ":3389" | find "ESTABLISHED" netstat -n | find ":3389" | find "ESTABLISHED" netstat -n | find ":3389" | find "ESTABLISHED" because it gives me all client connected and not only the one i need.

Thanks!

This can be achieve by using $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables of PHP.

The below both functions are equivalent with the difference only in how and from where the values are retrieved.

In first function have used getenv() is used to get the value of an environment variable in PHP

// Function to get the client ip address using getenv() function

    function get_client_ipaddress() {
     $ip_address = '';
     if (getenv('HTTP_CLIENT_IP'))
         $ip_address = getenv('HTTP_CLIENT_IP');
     else if(getenv('HTTP_X_FORWARDED_FOR'))
         $ip_address = getenv('HTTP_X_FORWARDED_FOR'");
     else if(getenv('HTTP_X_FORWARDED'))
         $ip_address = getenv('HTTP_X_FORWARDED');
     else if(getenv('HTTP_FORWARDED_FOR'))
         $ip_address = getenv('HTTP_FORWARDED_FOR');
     else if(getenv('HTTP_FORWARDED'))
        $ip_address = getenv('HTTP_FORWARDED');
     else if(getenv('REMOTE_ADDR'))
         $ip_address = getenv('REMOTE_ADDR');
     else
         $ip_address = 'UNKNOWN';

     return $ip_address; 
}

Without using getenv() function:

    function get_client_ipaddress() {
     $ip_address = '';
     if ($_SERVER['HTTP_CLIENT_IP'])
         $ip_address = $_SERVER['HTTP_CLIENT_IP'];
     else if($_SERVER['HTTP_X_FORWARDED_FOR'])
         $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
     else if($_SERVER['HTTP_X_FORWARDED'])
         $ip_address = $_SERVER['HTTP_X_FORWARDED'];
     else if($_SERVER['HTTP_FORWARDED_FOR'])
         $ip_address = $_SERVER['HTTP_FORWARDED_FOR'];
     else if($_SERVER['HTTP_FORWARDED'])
         $ip_address = $_SERVER['HTTP_FORWARDED'];
     else if($_SERVER['REMOTE_ADDR'])
         $ip_address = $_SERVER['REMOTE_ADDR'];
     else
         $ip_address = 'UNKNOWN';

     return $ip_address; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM