function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saravanan @CreationSaravanan @Creation 

To get the IP address of the local system.

Hi All,

 

when I am loading a visualforce page, I want the local system IP address to be printed on the screen .Is there any way  to do this?.

 

If any example with code will much appriciated.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
colemabcolemab
public static String GetUserIPAddress() {
	string ReturnValue = '';

	ReturnValue = ApexPages.currentPage().getHeaders().get('True-Client-IP');
			
	if (ReturnValue == '') {
		ReturnValue = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
	} // get IP address when no caching (sandbox, dev, secure urls)
			
	system.debug('USER IP ADDRESS: ' + ReturnValue);
		
	return ReturnValue;
		
} // GetUserIPAddress

 Above is some code to get you started.  You will need to add it to your controller or controller extension.

All Answers

colemabcolemab
public static String GetUserIPAddress() {
	string ReturnValue = '';

	ReturnValue = ApexPages.currentPage().getHeaders().get('True-Client-IP');
			
	if (ReturnValue == '') {
		ReturnValue = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
	} // get IP address when no caching (sandbox, dev, secure urls)
			
	system.debug('USER IP ADDRESS: ' + ReturnValue);
		
	return ReturnValue;
		
} // GetUserIPAddress

 Above is some code to get you started.  You will need to add it to your controller or controller extension.

This was selected as the best answer
Saravanan @CreationSaravanan @Creation

Thanks for your reply,

 

can you brefiely explain me what is caching.And I want the Mac address of the local system.

colemabcolemab

For caching on force.com, see this documentation.

 

I don't think you will be able to display the MAC address of the local system for technical reasons.  Basically, the MAC address is at the data link layer of the OSI model and as such won't be accurate at the next hop down from the orignal machine as the next device (host/gateway/router) will put their MAC address in that place. 

 

I could be wrong and the MAC address could be in a header but I seriously doubt it.

Andrew B. DavisAndrew B. Davis
There's nice code for this from Anthony Coleman (http://www.forcedisturbances.com/2012/05/limiting-access-to-visual-force-pages.html).
public static String GetUserIPAddress() {
 string ReturnValue = '';  
   
 // True-Client-IP has the value when the request is coming via the caching integration.
        ReturnValue = ApexPages.currentPage().getHeaders().get('True-Client-IP');
 
          // X-Salesforce-SIP has the value when no caching integration or via secure URL.
        if (ReturnValue == '' || ReturnValue == null) {
         ReturnValue = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
        } // get IP address when no caching (sandbox, dev, secure urls)
 
         if (ReturnValue == '' || ReturnValue == null) {
         ReturnValue = ApexPages.currentPage().getHeaders().get('X-Forwarded-For');
        } // get IP address from standard header if proxy in use
 
 return ReturnValue;
  
} // GetUserIPAddress

 
Lawrence-AccentureLawrence-Accenture
Any suggestions on how to do this if ApexPages.currentPage() isn't defined? (I.e. under Execute Anonymous or JavaScript remoting?)
Matt Matt 
In Execute Anonymous or Remote Actions the running user IP address is available from the session management class:
String ip = Auth.SessionManagement.getCurrentSession().get('SourceIp');