Search code examples
phpformsfeedback

Obtaining IP when someone posts a feedback


I want to get the IP of someone that posts a comment/feedback and save it in the database. Here is the code:

<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";
?>


Can someone please tell me what can I use and how to make it get the IP? (I know how to make it store in the database, but I don't know how to make it get the IP of the commenter).


Solution

  • You can get the users IP with $_SERVER['REMOTE_ADDR'] in php

    For additional information, I refer to the answer here, also recommended by @Matt Holbrook-Bull