2

I'm open to using SSJS or AMPscript to find the IP address of the client who is accessing a landing page. I tried this AMPscript:

%%=HTTPRequestHeader("X-Forwarded-For")=%%

Which resulted in a blank page.

6

Hmmm, that should work but it looks like it's not for some reason. I've tried a few different HTTP header fields and the HTTPRequestHeader() AMPscript function is not returning any of them.

Never mind. As an alternative approach, I'd suggest using an external service to retrieve the client IP address. There are plenty of them out there but ipify.org looks suitable as it's super lightweight, it doesn't have any rate limiting and supports both IPv4 and IPv6 addresses.

Simply use:

<p>Your IP address is: %%=HTTPGet('https://api.ipify.org')=%%</p>

Which displays:

Your IP address is: 136.147.128.12

136.147.128.12 belongs to Salesforce, so this won't work with landing pages either. See below for solution.

CloudPages

While this should work (it doesn't) on a landing page, it doesn't appear to work on a CloudPage as it appears that the CloudPage CDN is making a reverse proxy request and displays an IP address used by the data center hosting the CloudPage, and not the client IP.

If you are using landing pages (available on earlier accounts) then you should be fine, but CloudPages won't work and I can't come up with a solution for this. I've tried using SSJS, and:

HTTPHeader.GetValue("Host");

Will return the Host value in the HTTP header, but the following HTTP headers return the same empty value as its HTTPRequestHeader() AMPscript counterpart:

HTTPHeader.GetValue("Forwarded");
HTTPHeader.GetValue("X-Forwarded-For");

Solution

As we seem to have exhausted our server-side options for CloudPages, the simplest/only solution would be to:

  1. Make a client-side request to retrieve the IP address from ipify.org and display it on your page
  2. Store the IP address in a cookie
  3. Retrieve the cookie using AMPscript.

For purposes of my example, I'm just printing the IP address on the page, but you can use it to update a Data Extension, etc. I'm assuming that you are not adding any other cookies on your page. If you are, then you will need to update the Replace() function accordingly.

  <script type="text/javascript">
    var url = 'https://api.ipify.org';
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
        if (request.readyState === 4) {
            if (request.status === 200) {
                document.body.className = 'ok';
                document.cookie = 'ipAddress=' + request.responseText;
            } else {
                document.body.className = 'error';
            }
        }
    };
    request.open("GET", url , true);
    request.send(null);
    </script> 

   %%[ 
   var @ipAddress
   set @ipAddress = Replace(HTTPRequestHeader('Cookie'),'ipAddress=','')
  ]%%

   <p>IP Address: %%=v(@ipAddress)=%%</p>
  • @Daniel, I'm guessing you probably want to use the client IP address in AMPscript, so I've updated my answer to reflect this. – Eliot Harper Aug 22 '16 at 8:06
  • @Daniel, did this answer your question, or do you need more information? – Eliot Harper Aug 25 '16 at 17:43
  • I was away on vacation and what a surprise to come back to! Thank you! The purpose of the page is to redirect if the IP address falls outside a specific list. It's to help control who sees the landing page (internal vs. external to the organization.) – Daniel Aug 31 '16 at 13:17
3

I ran across the same problem today. In the end I found that, in SSJS, you can use

Platform.Request.ClientIP

which returns the client IP address as a string. Worked for me on a Landing Page.

See also: https://help.marketingcloud.com/en/documentation/server_side_javascript/server_side_javascript_syntax_guide/platform_server_side_javascript_functions/retrieve_http_properties_using_the_request_object/

  • This is the best and simplest way to go to retrieve the client IP address (or any other http request header) in a CloudPage/Landing page context. – Josh de Blank Sep 11 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.