English 中文(简体)
AJAX表格提交后,Get IP 访客地址
原标题:Get IP address of visitor after an AJAX form submit

I have an HTML form. When visitor submits form, a javascript method is invoked. This method sends an AJAX request to my server s php file. My problem is i need to get the visitor s ip address. But because of AJAX request calls php file, $_SERVER[ REMOTE_ADDR ] gives me my server s address. How can i get visitor s ip, in this case? 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]将给您客户的IP地址。 但是,由于你大概使用服务器和<>/strong>客户的同一机器,你获得的也是正常的。 一旦你将网站作为网络服务器,并且从不同的机器中远程访问,你将获得该遥远机器的地址。

因此,你无需做些什么。 你的法典已经按照预期发挥作用。

问题回答

jax要求仍然来自客户,应当给予客户IP而不是服务器。

通过以下方式尝试守则,因为它意在上载上该页,它使用舱位和一些免费的皮页。

$(document).ready(function(){
  $.getJSON("https://api.ipify.org?format=json", function(data){
    var userIP = data.ip;
    $.getJSON("https://api.ipgeolocationapi.com/geolocate/" + userIP, function(data){
      var userLat = data.geo.latitude;
      var userLng = data.geo.longitude;
      // You can now use the user s latitude and longitude to determine if they re within your geo fence
    });
  });
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.