简体   繁体   English

如何使用PHP从HTML表单获取IP地址?

[英]How to get IP address from HTML form using PHP?

For now, I'm trying to receive the ip address of a user that uses a contact form on a website and send that to firebase along with other information. 目前,我正在尝试接收使用网站上联系表格的用户的IP地址,并将其与其他信息一起发送到Firebase。 For the HTML and PHP, I used the exact code from this tutorial: http://www.danielpinero.com/how-to-add-ip-address-html-form . 对于HTML和PHP,我使用了本教程中的确切代码: http : //www.danielpinero.com/how-to-add-ip-address-html-form

Here is the firebase code: 这是firebase代码:

    // Initialize Firebase (ADD YOUR OWN DATA)
var config = {
    apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXX",
    authDomain: "XXXXXXXXXXXXXXXXXXXXXXXX",
    databaseURL: "XXXXXXXXXXXXXXXXXXXX",
    projectId: "XXXXXXXXXXXXXXXXX",
    storageBucket: "XXXXXXXXXXXXXXXXX",
    messagingSenderId: "XXXXXXXXXXXX"
  };
  firebase.initializeApp(config);

// Reference messages collection
var messagesRef = firebase.database().ref('messages');

// Listen for form submit
el = document.getElementById('contactForm')
if(el){
  el.addEventListener('submit', submitForm);
}

// Submit form
function submitForm(e){
  e.preventDefault();

  // Get values
  var name = getInputVal('name');
  var email = getInputVal('email');
  var message = getInputVal('msg');
  var ip = getInputVal('ip')

  // Save message
  saveMessage(name, email, message, ip);

  // Clear form
  document.getElementById('contactForm').reset();
}

// Function to get get form values
function getInputVal(id){
  return document.getElementById(id).value;
}

// Save message to firebase
function saveMessage(name, email, message, ip){
  var newMessageRef = messagesRef.push();
  newMessageRef.set({
    name: name,
    email:email,
    message:message,
    ip:ip
  });
}

This is the output im receiving from firebase when clicking submit on my form: 这是我在表单上单击“提交”时从firebase收到的即时消息: 在此处输入图片说明

Instead of this, I'd like the user's actual IP to show up. 取而代之的是,我希望显示用户的实际IP。 I'm very new to PHP. 我是PHP的新手。 It'd be great if someone could help me out. 如果有人可以帮助我,那就太好了。

EDIT (index.php): 编辑(index.php):

<form id="contactForm" method="post" class="cta">
                <div class="col gtr-uniform gtr-50">
                    <div class="col-8 col-12-xsmall inputs"><input type="text" name="name" id="name" placeholder="Name" /></div>
                    <div class="col-8 col-12-xsmall inputs"><input type="email" name="email" id="email" placeholder="Email Address" /></div>
                    <div class="col-8 col-12-xsmall inputs"><textarea id="msg" rows="4" cols="50" placeholder="Message"></textarea></div>
                    <input id="ip" type="hidden" name="ip" value="<?=$ip;?>">
                    <div class="col-4 col-12-xsmall"><input type="submit" value="Get Started" class="fit primary" /></div>
                </div>
            </form>

The first issue is that you are missing a semicolon near var ip = getInputVal('ip') 第一个问题是您在var ip = getInputVal('ip')附近缺少分号

The second issue is to change this <input id="ip" type="hidden" name="ip" value="<?=$ip;?>"> to <input id="ip" type="hidden" name="ip" value="<?php echo $ip;?>"> Use this for getting better results for client IP: 第二个问题是将<input id="ip" type="hidden" name="ip" value="<?=$ip;?>">更改为<input id="ip" type="hidden" name="ip" value="<?php echo $ip;?>">使用此命令可获得更好的客户端IP结果:

<?php
function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
    $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
    $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
    $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
    $ipaddress = $_SERVER['REMOTE_ADDR'];
else
    $ipaddress = 'UNKNOWN';
return $ipaddress;
} 
$ip = get_client_ip();
?>

Now as for the js variable you can fill it in various ways: 现在,对于js变量,您可以通过多种方式填充它:

<script>var ip = "<?php echo $ip;?>"; </script>

Or you can fill it to the value of a hidden input and get the value though js: 或者,您可以将其填充为隐藏输入的值,然后通过js获取该值:

<script>var ip = document.getElementById('inputID').value; </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM