Get Remote IP Address in PHP


SUBMITTED BY: Guest

DATE: Aug. 25, 2012, 10:04 p.m.

FORMAT: PHP

SIZE: 678 Bytes

HITS: 1602

  1. function getRemoteIPAddress() {
  2. $ip = $_SERVER['REMOTE_ADDR'];
  3. return $ip;
  4. }
  5. The above code will not work in case your client is behind proxy server. In that case use below function to get real IP address of client.
  6. function getRealIPAddr()
  7. {
  8. if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
  9. {
  10. $ip=$_SERVER['HTTP_CLIENT_IP'];
  11. }
  12. elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
  13. {
  14. $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  15. }
  16. else
  17. {
  18. $ip=$_SERVER['REMOTE_ADDR'];
  19. }
  20. return $ip;
  21. }

comments powered by Disqus