Get Remote IP Address in PHP

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

[View All Snippets]
Show Plain Text »
  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. ?>
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory






Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.