Hello Everyone, After going through this tutorial, you will find a code example that shows, how to get IP Address in the Visualforce page using an Apex controller. In many applications, there will be a requirement in which you have to track IP addresses so this example will help you lot to get rid of this problem.

After completing this tutorial, you'll able to:
  • Track IP Address using Visualforce and Apex.
So let's start, 
 
Please follow below steps to accomplish this task.
 
Step 1: Login to your Salesforce Org. and open developer console.

Step 2: Navigate to File | New | Apex Class and create an apex class called GetIPController and replace the following code.

GetIPController.apxc
  public class GetIPController {
  public String ipAddress {get; set;}
    //Constructor
    public GetIPController(){    
        
        // True-Client-IP has the value when the request is coming via the caching integration.
        ipAddress = ApexPages.currentPage().getHeaders().get('True-Client-IP');
        
        // X-Salesforce-SIP has the value when no caching integration or via secure URL.
        if (ipAddress == '' || ipAddress == null) {
            ipAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
        } 
        
        // get IP address when no caching (sandbox, dev, secure urls)        
        if (ipAddress == '' || ipAddress == null) {
            ipAddress = ApexPages.currentPage().getHeaders().get('X-Forwarded-For');
        }
        
        //get IP address from standard header if proxy in use
        //ipAddress = ApexPages.currentPage().getHeaders().get('True-Client-IP');        
    }
 } 

Step 3: Navigate to File | New | Visualforce Page and create a visualforce page called GetIPAddress and replace the following code.

GetIPAddress.vfp
 <apex:page controller="GetIPController">
    <center>
        <br/><br/><br/>
        <span style="font-size:19px;">IP Address in Visualforce Page</span><br/><br/>
        <span style="font-size:17px;">
           {!ipAddress}
        </span>
    </center>
 </apex:page>
Output:
 
See also:
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.