Posted By

apphp-snippets on 02/12/13


Tagged

php remote ip address


Versions (?)

Get Remote IP Address in PHP


 / Published in: PHP
 

URL: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address

This code allows to get the IP address from which the user is viewing the current page.

  1. <?php
  2. function getRemoteIPAddress(){
  3. $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
  4. return $ip;
  5. }
  6.  
  7. /* If your visitor comes from proxy server you have use another function
  8. to get a real IP address: */
  9. function getRealIPAddress(){
  10. if(!empty($_SERVER['HTTP_CLIENT_IP'])){
  11. //check ip from share internet
  12. $ip = $_SERVER['HTTP_CLIENT_IP'];
  13. }else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  14. //to check ip is pass from proxy
  15. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  16. }else{
  17. $ip = $_SERVER['REMOTE_ADDR'];
  18. }
  19. return $ip;
  20. }
  21. ?>

Report this snippet  

You need to login to post a comment.