[英]Get IP address of visitor after an AJAX form submit
I have an HTML form. 我有一个HTML表单。 When visitor submits form, a javascript method is invoked. 当访问者提交表单时,会调用javascript方法。 This method sends an AJAX request to my server's php file. 此方法将AJAX请求发送到我的服务器的php文件。 My problem is i need to get the visitor's ip address. 我的问题是我需要获取访问者的IP地址。 But because of AJAX request calls php file, $_SERVER['REMOTE_ADDR'] gives me my server's address. 但由于AJAX请求调用php文件, $ _SERVER ['REMOTE_ADDR']给了我服务器的地址。 How can i get visitor's ip, in this case? 在这种情况下,我如何获得访问者的IP? Thank you 谢谢
<form onsubmit="sendData(); return false;">
// some data here
</form>
function sendData(){
// do some work, get variables
$.ajax({
url:"/mypage.php",
type:"GET",
data: { name: e },
success : function(data) {
// do some work
},
error: function (xhr, ajaxOptions, thrownError) {
}
})
}
// in mypage.php
public function useData() {
$name=$_GET["name"];
$ip = $_SERVER['REMOTE_ADDR'];
}
$_SERVER['REMOTE_ADDR']
will give you the IP address of the client. $_SERVER['REMOTE_ADDR']
将为您提供客户端的IP地址。 But since presumably you are using the same machine as server and client you get the same IP which is normal. 但是,由于您可能使用与服务器和客户端相同的计算机,因此您可以获得相同的IP,这是正常的。 Once you host your website into a web server and access it remotely from a different machine you will get the address of that remote machine. 将网站托管到Web服务器并从另一台计算机远程访问后,您将获得该远程计算机的地址。
So there's nothing more you need to do. 所以你不需要做更多的事情。 Your code already works as expected. 您的代码已按预期工作。
ajax请求仍然来自客户端,它应该给客户端IP而不是服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.