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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?