-3

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.

Yush
  • 48
  • 10
  • The IP _must_ be getting passed in with the request on the server-side. If it's not then there must be something misconfigured or you're not doing it correctly. You shouldn't be doing it through JS. – VLAZ Nov 03 '16 at 12:07
  • `"But it's not giving the result I want."` - Well, what is the result you want? What is the result it's giving you? At what point does it become incorrect? Since you're basically asking users to tell you their IP, they can tell you anything they want. There's no guarantee that you're going to get a correct IP address, or even any IP address at all. – David Nov 03 '16 at 12:09
  • Possible duplicate of [Set the value of a input field with javascript](http://stackoverflow.com/questions/7609130/set-the-value-of-a-input-field-with-javascript) – RST Nov 03 '16 at 12:09
  • *But the php code $ip = $_SERVER['REMOTE_ADDR']; is not showing the actual vistor ip* - I don't see that anywhere in your code, nor do I see any php. – Funk Forty Niner Nov 03 '16 at 12:12
  • @RST I disagree. That is an answer of a very narrow part of the question but I believe this is an XY problem. – VLAZ Nov 03 '16 at 12:12
  • @vlaz as I am not able to get the correct ip through php I would like JavaScript doing this in my case so How could I pass that ip value to the next page (sent.php) probably with ajax post request? Any answer would be great – Yush Nov 03 '16 at 12:22
  • @David I know users can show me the wrong ip but I am counting it for normal users not for any hacker or geek – Yush Nov 03 '16 at 12:24
  • @Fred-ii- It's not a question for getting the ip with php, I have tried all the possible php method I could, used the curl and get_file_content and many classes but I see my own ip is showing wrong and using the same code on a different server shows it correct so I don't wanna do it with php – Yush Nov 03 '16 at 12:27
  • 1
    Question is: *"what will you do if users have js disabled; what then?"* - If you're going to want to get their IP address with JS, you should have a serverside method as a "Plan B". If PHP failed you, then you need to find out why that is and if it's related to possible errors somewhere. That's where error reporting may be of help here. – Funk Forty Niner Nov 03 '16 at 12:30
  • I also don't see where you're using/calling the `getIP()` function. Question's unclear in many ways, for me anyway, Your question/comments are a bit confusing. – Funk Forty Niner Nov 03 '16 at 12:31
  • @ŸüšhäKĥãn: The code shown in the question works when I test it. If something specific isn't working for you, you're going to have to elaborate on the problem. "It doesn't work on my machine" doesn't really tell us anything useful. – David Nov 03 '16 at 12:33
  • @Fred-ii- why should I post the unnecessary codes which I don't want the answer of, I am looking to complete this task with javascript instead of php, and if the user's disable js they have to turn it on to use the contact form else they can not send me any mails as javascript form validation will also not work in this case. – Yush Nov 03 '16 at 12:34
  • @David on the sent.php page I am able to echo all the post values but not the last form input which is for ip, I want this to auto complete from the value of the JavaScript code and submit it as a post value to the next page (sent.php) – Yush Nov 03 '16 at 12:38
  • @ŸüšhäKĥãn: If you can specify what's actually failing, we can help with that. If you only specify your requirements and want someone to build it for you, then your best bet at that point is to hire a programmer. None of us can see your screen from here nor can we debug this on your computer for you. You're going to have to put in at least *some* effort. – David Nov 03 '16 at 12:43
  • @David bro I am giving effort I am not asking to build it for me but am I on the correct path? I mean are my codes are correct? I am getting the blank value for ip in the sent.php page – Yush Nov 03 '16 at 12:49
  • @ŸüšhäKĥãn: Nobody here can tell you anything about your sent.php page. If something on *that* page is failing, then you really should take a look at *that* page. However, we can't know if what's failing is on this page or on that one because you haven't been able to tell us what's actually failing. Is the value sent in the form post at all? Is it set in the form field? Is it correct in the `jsvar` variable, or in `json.ip` to begin with? Is the JavaScript throwing an error on the console? Is the external JavaScript even loading? "Putting in some effort" is more than just asking us to fix it. – David Nov 03 '16 at 12:57
  • @vlaz his question is about getting information into a (hidden) form field using javascript. That part is perfectly covered in the link mentioned. The debat on whether or not the scenario is wrong doesn't change that. – RST Nov 03 '16 at 13:31
  • @RST bro can you post a solution on this question so I could mark it as correct answer – Yush Nov 03 '16 at 13:33
  • when you `alert(jsvar);`, is the ip present? – RST Nov 03 '16 at 16:48

1 Answers1

1

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>
Yush
  • 48
  • 10