Get your Visitor’s IP address via sites

Standard

Have you ever ran into a situation, where you need to figure out your site’s visitor’s IP address? Well, many of us may or may not have. But the fact is, while working with sites, sooner or later you will bump into this requirement.

So, like every requirement, even this one can be dealt with, in many different ways. Like

1. Use an existing program like http://jsonip.com or http://jsonip.appspot.com/
2. Write a program (which will return visitor ip) in your choice of language (like php, asp, jsp, etc..) and put it on a web server.
3. Create a public vf page and use apex in the background, to get your visitor’s ip address.

I wouldn’t suggest option # 1 and 2 as they may or may not be available when your visitors come to your site. But if you go with option 3
then its almost certain that your visitor ip address can be traced. Because both your site and ip tracing vf page will be on sfdc servers. So, if a visitor is able to see your site that mean you can trace their ip address, for sure.

Here is the sample code:

VF page: getip


<apex:page controller="getipController" showheader="false" contenttype="application/json">{!ipAddress}</apex:page>

 

Apex class getipController


public class getipController {
 public String ipAddress {get; set;}
 public getipController(){
 ipAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
 }
 }

 

Now that you can have code in place. Go ahead and make this page public from your force.com site.

That’s it. Your ip tracing page is now ready to be used in your site.

Using jQuery, you could make a JSON call:




$.getJSON("your page url", function(data){alert(data);});



 

Hope that’s helpful.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s