I have implemented AMP email, so when the client submits any data via amp-form
from inside of the email body, the request goes to google and google submits data to my server via an HTTP request but as AMP requests come to my server from Google server instead of the client’s browser, I am not able to get Client’s IP via normal PHP $_SERVER
variable.
Is there any workaround for AMP EMAILS by which I can Client’s IP in my PHP Apache backend?
Below is the code I am using to get IP as of now, but not able to get client IP in any of the $_SERVER
key:
function get_ip_address()
{
$str="";
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
if (array_key_exists($key, $_SERVER) === true)
{
foreach (explode(',', $_SERVER[$key]) as $ip)
{
$ip = trim($ip); // just to be safe
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
{
$str .= $key." => ".$ip." ";;
}
}
}
}
return $str;
}
Edit:
As per Su Zhang‘s comment over his answer, we can not get IP of the user due to privacy concerns, So a solution/workaround where I can get a nearby IP as per geolocation of the client will also work for me.
2
Answers
AMP email providers don’t disclose the IP address of the user or else it would be a privacy leak. There’s no way you can get the IP address of the user via form submission.
The easiest way to do this is to pull a JSON file for an empty function and attach it to the user.
Even if the user doesn’t submit anything, if the user "activates" something in the AMP email that triggers this pull, you will have the IP.