有人发布反馈时获取IP

时间:2014-12-13 14:45:34

标签: php forms feedback

我希望获得发布评论/反馈并将其保存在数据库中的人的IP。这是代码:

<form action="feedback.php" method="POST">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="name">Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="comment">Message *</label>
 </td>
 <td valign="top">
  <textarea  name="comment" maxlength="1000" cols="25" rows="6"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" name="add" value="Add FeedBack"> 
 </td>
</tr>
</table>
</form>

<?php
    if(isset($_POST['add'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];
        if($name){
            if($email){
                if($comment){
                    mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','$name','$email','$comment')");
                }
                else
                    echo "You haven't entered any comment!";
            }
            else
                echo "You haven't entered an email address!";
        }
        else
            echo "You haven't entered your name!";
    }
?>

<?php
    $run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
    $numrows = mysql_num_rows($run);
    if($numrows > 0){
        while($row = mysql_fetch_assoc($run)){
            $dbname = $row['name'];
            $dbcomment = $row['comment'];
            echo "Commented By $dbname<br>$dbcomment<br><br>";
        }
    }
    else
        echo "<br>There are no feedbacks made";
?>


有人可以告诉我我可以使用什么以及如何使其获得IP? (我知道如何将其存储在数据库中,但我不知道如何让它获得评论者的IP)。

2 个答案:

答案 0 :(得分:1)

您可以在php

中使用$_SERVER['REMOTE_ADDR']获取用户IP

有关其他信息,请参阅@Matt Holbrook-Bull推荐的答案here

答案 1 :(得分:0)

使用$_SERVER['REMOTE_ADDR']超全球。