0

So I have an AMP form that, depending on where the user is, needs to redirect them to different places. I have a set-up that uses two hidden fields likes so:

<input name="ip_address" type="hidden" value="<?php echo getRemoteIPAddress()?>">
<input name="lead_geo" type="hidden" value="<?php echo getGeoByIp()?>">

Along with corresponding functions that I know work correctly as they've been tried and tested throughout the rest of the site.

Then I have my endpoint set up with a qualifier like so:

if($lead_geo == '' || $lead_geo == 'CA' || $lead_geo == 'AZ' || $lead_geo == 'IL' || $lead_geo == 'TX' || strlen($lead_geo) > 5) {
     header("AMP-Redirect-To: ".$location-specific-destination);
} else {
     header("AMP-Redirect-To: ".$default);
}

And this all works perfectly locally, on stage, and on production until the amp page is cached. The problem is Google caches the first IP that requests it. Is there a way of getting a users location on the cached side? Or even if I could just get their IP on the page I could have the endpoint get the location from it.

TLDR: How can I get a user's location or even just IP address when they submit an AMP form.

Mitch
  • 107
  • 1
  • 2
  • 9

1 Answers1

2

You need to get the user's IP when they submit the form. The cached version will not contain the user's original HTTP info. You will have to obtain the location on the server, using the IP using the header from the form submission, not in the form fields themselves (which are now invalid).

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • Gotcha, that makes sense. So I gotta get their IP from the header from the form submission. How do I do that in PHP? – Mitch Apr 30 '19 at 18:01
  • Like this: https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php – Diodeus - James MacFarlane Apr 30 '19 at 18:02
  • Oh, so it's no different from if it was a page they just landed on it's just accessing it from the endpoint, so I just basically just need to move the IP code from the form page to the endpoint. Thanks, I'll try that! – Mitch Apr 30 '19 at 18:06