从Javascript获取访客IP,然后将其用作php变量

Hello I am developing a contact form which will give the user ip and geo location details while they send me the message from my website, But the php code $ip = $_SERVER['REMOTE_ADDR']; is not showing the actual vistor ip, I have tried with many php functions they are working fine on other hosts but in my host it's not properly showing the client ip So I have decided to get the ip using JavaScript and then when my visitors will submit the contact the form to sent.php page the ip which I got from the JavaScript will be input with the form to get the POST value from it then I can process that variable and get the geo location and other information and send it along php mail.

Here is the javascript to get the actual user ip

    <script type="application/javascript">
      function getIP(json) {
        document.write("My public IP address is: ", json.ip);
      }
    </script>
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP">
</script>

And my email contact form is

    <form name="contact" class="select-style" action="sent.php" method="POST" >
            <p>Hello


            <select name="recipient">
            <option value="recipient_0" selected>Please Select Recipient !</option>             
                    <option value="recipient_1">Sales</option>
                    <option value="recipient_2">Support</option>
                </select>           

            </p>
            <label for="email">Your Message : </label>
            <textarea  name="message" value="Your Message" id="message" ></textarea>
            <p>Regards,</p> 
            <label for="name">Name: </label>
            <input type="text" name="name" value="" id="name" />
            <label for="email">Email: </label>
            <input type="text" name="email" value="" id="email" />
            <input type="hidden" name="ip" value="">
            <input type="submit" name ="submit" value="OK I want to send it now, thanks!" />
        </form>

And I think this is how I should input the ip with the form

<script type="text/javascript">
    jsvar= json.ip;
    document.contact.ip.value = jsvar;
</script>

But it's not giving the result I want. Please help.

Solved the issue.

I just need to insert these JavaScript codes after the hidden input in <from>

    <input type="hidden" name="ip" value="" id="ip" />


 <script type="text/javascript">
  var userip;
</script>

<script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>


                <script type="text/javascript"> 
  document.getElementById("ip").setAttribute('value', userip);
</script>