在提交表单时包含客户的IP

问题描述 投票:0回答:1

当客户在我的网站上提交表单时,我需要包含客户的 IP 地址?这可以使用 python 或 javascript 而不使用任何第三方脚本吗?将 IP 地址与表单详细信息一起传递。

javascript html forms python-3.x
1个回答
0
投票

添加一个输入字段,并将其设置为不显示。

<input type="text" name="IP Address" id="visitor_ip-2" value="IP" style="display: none;">

添加此js代码来获取IP地址

  // Function to retrieve the visitor's IP address
  function getVisitorIP2(callback) {
var xhr2 = new XMLHttpRequest();
xhr2.open('GET', 'https://api.ipify.org?format=json', true);
xhr2.onreadystatechange = function() {
  if (xhr2.readyState === 4 && xhr2.status === 200) {
    var response = JSON.parse(xhr2.responseText);
    callback(response.ip);
  }
};
xhr2.send();
}
// Populate the hidden field with the visitor's IP address
  getVisitorIP2(function(ip) {
document.getElementById('visitor_ip-2').value = ip;
  });
© www.soinside.com 2019 - 2024. All rights reserved.