PDA

View Full Version : Php, log-ip tutorial


shwaza
01-24-2005, 10:46 PM
Have you ever had a geustbook, or a shoutbox, or anything else that can be publically posted in? Did people spam it, but you didn't know who they were so you couldn't block them out of your site? Well, today, professor shwaza is going to teach you how to log a person's ip, so you can lock them out of your site for good :)

Step 1: Tables/Databases

Alright, we're going to assume that you have already made a table for the script you have, it would probably have like, email address, message, or whatever sort of information you're trying to take in from the user. Ok, now, you are going to go and add a colom (field) to this table, and call it "ip". Make it varchar, 255 charactors.

Step 2: Altering your query

Ok, now you are going to edit your php/mysql query that you are making on the page in which people are inputing data to you. First, you are going to set the variables of the new field you are inputing. Up above the actual query itself, you should see something like
$content=$_POST['content']; Except content would most likley be named something else. What you are going to do, is add a line, right under that, and type in this
$ip=$_POST['ip'];
Next, you are going to alter the actual query. It should look something like this already.
$results=mysql_query("INSERT INTO tablename (field1, field2)
VALUES ('$field1', '$field2')")
Keep in mind also, that there may be more than just 2 fields ;) What you are going to do, is tell mysql, that it needs to insert data into the field ip, so you are going to alter your code to look like this
$results=mysql_query("INSERT INTO tablename (field1, field2, ip)
VALUES ('$field1', '$field2', '$ip')")
Now, mysql knows that you want to insert data into 3 fields, (more if your script already has more than 2 fields)

Step 3: Inserting the ip
Alright, now, we can leave our query alone, and go to the form, in which we are inserting our data from. It should look something like this, (and again, there will be more inputs, if you have more fields).
<form action="somepage.php" method="post">
Email:<br><input type="text" name="name" value=""><br>
Content:<br><textarea name="content" cols="50" rows="15"></textarea><p>
<input value="Submit!" type="submit" name="submit">
</form>
Ok, so you should have something relativley similar to that. Now, we just add in one more input to that, to log the ip. It would look like this:
<form action="somepage.php" method="post">
Email:<br><input type="text" name="email" value=""><br>
<input type="hidden" name="ip" value="<? echo $_SERVER['REMOTE_ADDR']; ?>">
Content:<br><textarea name="content" cols="50" rows="15"></textarea><p>
<input value="Submit!" type="submit" name="submit">
</form>
That is a hidden input that the viewer of the page cannot see, and it will have a value of their ip address.

Now you are done! Your posters ip addresses will now be logged into your table along with their post, and whatever information you are collecting from them :)

Tim
01-25-2005, 12:04 AM
I guess you could do it this way, but with FireFox and a couple of extensions, you can easily edit the content of hidden form fields: and this method is vulnerable to sites submitting false IP addresses from across the internet, and anyone who has a pinch of hacking skills would know that.
The better way to do it would be to get rid of the hidden form field, and in the form processor, just put $ip = $_SERVER['REMOTE_ADDR']. This way, the server processes it, and there is no way for the end-user to change the contents of their IP address, without hacking the server.

Good Try Though :P,
Tim

shwaza
01-25-2005, 12:10 AM
Lol tim, you and your... brain :P You know too much. Also though, just out of interest, how do you alter stuff in a hidden form field?