当客户在我的网站上提交表单时,我需要包含客户的 IP 地址?这可以使用 python 或 javascript 而不使用任何第三方脚本吗?将 IP 地址与表单详细信息一起传递。
添加一个输入字段,并将其设置为不显示。
<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;
});