0

I need a code that displays the IP of the person/guest that commented on my website. Here's the index page;

<?php
include "core/init.php";

?>


<!DOCTYPE html>
   <link rel="stylesheet" type="text/css" href="http://hjalplista.comxa.com/daniel_emelie/style.Css">

<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Välkommen till hjälplistan!</title>



<body>
<div id="header">
<div align="right"><a href="http://www.helsingborg.se/wieselgrensskolan" target="_blank"><img src="http://i.imgur.com/y0aiNIt.jpg"></a></div>
</div>

<div id="inside">

<center>

<div id="location"><a href='index.php'>Hem</a> - <a href='administrative/adminonlyaccess.php'><i>Admin</i></a><br><br>
</div>
<hr/>


<br>


<div id="text">
<br>

<table border="1" cellpadding="1" cellspacing="1" style="width: 100%;">
    <tbody>
        <tr>
            <td><span style="color:slategray;"><strong><center>Nr</center></strong></span></td>
            <td><span style="color:slategray;"><strong><s><center>Fått hjälp</center></s></strong></span></td>
            <td><span style="color:slategray;"><strong><center>Namn</center></strong></span></td>
            <td><span style="color:slategray;"><strong><center>Uppgift</center></strong></span></td>
            <td><span style="color:slategray;"><strong><center>IP-Adress</center></strong></span></td>
        </tr>

<?php
include "core/inc/conn.php";

mysql_select_db("comments");

$getquery = mysql_query("SELECT * FROM comments ORDER BY id DESC");
while($rows = mysql_fetch_assoc($getquery))
{


     $id = $rows['id'];
     $comment_name = $rows['name'];
     $comment = $rows['comment'];



     echo"<tr><th><font color='lightgray'>$id</font></th>";
     echo"<th><font color='red'>Fungerar ej!</font></th>";
     echo "<th><u><font color='black'>$comment_name</font></u></th>";
     echo"<th><font face='black'>$comment</font></th>";
     echo"<th></th></tr>";







 }

    if(isset($_GET['error']))
{
    echo "<p>15 Bokstäver max!";

}


?>

</table>

<br><br>
</body>
<head>
<center>


</div>

<br><br>


<form action="post_comment.php" method="post">

<label> Namn: <input type="text" name="name"></label><br><br>
<label> Uppgift: <input type="text" name="comment"></label><br><br>
<input type="submit" name="post" value="Skicka">

</form>



<br><br>


<br><br>

<?php
echo "<br><br><br>";
echo "<hr/>";
echo "<p> &copy; Wieselgrenskolan. All rights reserved. </p>";

?>

</head>
</html>

<html>

<div align="right">

<!-- START OF HIT COUNTER CODE -->
<br><script language="JavaScript" src="http://www.counter160.com/js.js?img=11"></script><br><a href="http://www.000webhost.com"><img src="http://www.counter160.com/images/11/left.png" alt="Free web hosting" border="0" align="textright"></a><a href="http://www.hosting24.com"><img alt="Web hosting" src="http://www.counter160.com/images/11/right.png" border="0" align="texttop"></a>
<!-- END OF HIT COUNTER CODE -->

</div>
</html>

And it's supposed to be in the echo "<th></th></tr>";. Do I need a MySQL table for that?

I've created a index in mysql called ''commentip'' but I don't know how to fix the missing parts.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    What should that ip address be good for? Its a dynamic address anyway in 99% of all cases. Anyways: take a look at phps "superglobals", you find some request details in there. – arkascha Apr 06 '14 at 20:21
  • 1
    possible duplicate of [How to get Client IP address in PHP?](http://stackoverflow.com/questions/3003145/how-to-get-client-ip-address-in-php) – sshow Apr 06 '14 at 20:22
  • I need it to be able to ban their IP's. – user3504482 Apr 06 '14 at 20:23
  • *"How to retrieve the comment IP*" and *"I need it to be able to ban their IP's"* are two different animals altogether. Banning IP addresses is not always reliable. – Funk Forty Niner Apr 06 '14 at 20:24
  • What code is writing comments to the database? If it isn't saving the IP address with the comment, that information will not be stored anywhere, and there is no way to retrieve it. –  Apr 06 '14 at 20:26
  • Well I'm not creating a high-secure website and this website wasn't supposed to be a difficult one, I just need the IP to be displayed of the comment to be able to ban them, I'm doing a simple website for my teacher. – user3504482 Apr 06 '14 at 20:27
  • You would not believe the results Google gave while searching for "how to ban ip address php"; use it. – Funk Forty Niner Apr 06 '14 at 20:28
  • @duskwuff, Do you want the code of my post comment code? – user3504482 Apr 06 '14 at 20:28
  • @Fred-ii- I've got the code to be able to ban, I just need to get the IP address, displayed on a table. I just need the (get IP) code. :/ – user3504482 Apr 06 '14 at 20:30
  • Smistry's answer is what you should use. @user3504482 just do `echo $_SERVER['REMOTE_ADDR'];` – Funk Forty Niner Apr 06 '14 at 20:32
  • I am having a bit of trouble understanding what you mean by "get IP code... displayed on a table". You will need to be more precise. – Funk Forty Niner Apr 06 '14 at 20:33
  • Do you mean, you have an IP (or list of) stored in your DB and you would like to echo it in an HTML table? – Funk Forty Niner Apr 06 '14 at 20:34
  • @Fred-ii- If or when someone sends a comment, then it'll display his; ID/Number | Name | Comment | IP | I just don't know how to get the REMOTE_ADDR of the user that commented. :/ – user3504482 Apr 06 '14 at 20:36
  • You will need to first store the IP in a row associated with the user's id, then simply (fetch/loop) and add a row in your HTML table fetched from your DB. I.e.: `$user=$row['user']; $IP=$row['ip']; echo $user . "\n"; echo $IP . "\n";` – Funk Forty Niner Apr 06 '14 at 20:38
  • Yes, exactly, @Fred-ii-, but the problem is, I don't have that code. I need a code, that will save the user's comment's IP into my DB/MySQl and then I'd like to echo it out on my website! :) – user3504482 Apr 06 '14 at 20:39
  • banning based on ip, is like banning based on shirt colour( many people have same shirt colour, and changing shirt is easy) –  Apr 06 '14 at 20:40
  • `INSERT into table_name (id,ip,comment) VALUES ($id,$IP,$comment)` is the basic INSERT syntax. Using `$IP=$_SERVER['REMOTE_ADDR'];` – Funk Forty Niner Apr 06 '14 at 20:40
  • You could also add an extra colum called `allowed` and automatically make it as `YES` for example when writing a new entry to DB. Then, when you call your script, you would add a `WHERE` clause to the effect of `WHERE banned='NO'` to allow a user --- Your "banned" addresses would hold a `YES` instead of a `NO`. – Funk Forty Niner Apr 06 '14 at 20:50
  • @Fred-ii-, is there any chance you could help me out with just a simple line of code, like just really simple. Right now, I used $ip = $_SERVER['REMOTE_ADDR']; but now it only shows the 5 first numbers of the REMOTE_ADDR, without dots, for example; 12123 any help? – user3504482 Apr 06 '14 at 21:09
  • What is your column set to? VARCHAR, INT, other? Also, your column's length will also play a role. It needs to be a minimum of 16. If it's lower, set it to 25. – Funk Forty Niner Apr 06 '14 at 21:11
  • @Fred-ii-, it's an INT(11), should I change it? – user3504482 Apr 06 '14 at 21:38
  • Yes, change it to VARCHAR(25) just to be safe because there are dots and will not be interpreted correctly if set to INT. – Funk Forty Niner Apr 06 '14 at 22:11
  • I've got it right now, there's a problem that will be annoying to fix, currently everything's stable and it works! Oh by the way, had to change it to VARCHAR(100), though it works! :) Thanks. – user3504482 Apr 06 '14 at 23:08
  • Great, so problem solved then? @user3504482 – Funk Forty Niner Apr 06 '14 at 23:26

1 Answers1

0

You can get the IP in php in most cases using $_SERVER['REMOTE_ADDR'] However if they are using a proxy or you are using a reverse proxy like cloudflare or incapsula then you can use $_SERVER['HTTP_X_FORWARDED_FOR'].

smistry
  • 1,127
  • 1
  • 9
  • 9