Get Remote IP Address in PHP


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.