dongsheng1238 2016-11-03 12:05
浏览 75
已采纳

从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.

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-11-03 18:43
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程