2014-12-13 56 views
-1

我想獲得發佈評論/反饋的人的IP並將其保存在數據庫中。下面是代碼:當有人發佈反饋時獲取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

很好的答案在這裏http://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php – DevDonkey 2014-12-13 14:48:35

回答

1

你可以得到$_SERVER['REMOTE_ADDR']用戶的IP在PHP

有關其他信息,我指的是答案here,還建議由@馬特布魯克,公牛

+0

認爲是我的網站沒有登錄。反饋表單只是一種表單,希望留下反饋意見的人只需在其中填寫電子郵件,姓名和評論。我認爲我不能做太多事情。 – 2014-12-13 15:39:22

+0

當然可以,$ _SERVER與登錄無關。只是試圖迴應它@theHazeMaze – baao 2014-12-13 15:52:22

+1

哦,它的工作,非常感謝! – 2014-12-13 16:04:43

0

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