Finding an IP address in a form

From 118Wiki
Jump to navigation Jump to search

This tutorial will teach you how to find and record an IP address in a form. The purpose of finding someone's IP address is to track users definitively. While a user can report their e-mail address voluntarily via the form, or give a name of some type, it is quite easy to fudge this information and use whatever one wishes. An IP address cannot be faked without significant effort, and will give you a mostly reliable method of tracking the submitters of forms on your website.

For the purposes of this tutorial, we're going to assume that you are familiar with creating a web form.

Make sure it's .php

This method will only work if your form's file extension is .php (which processes php coding). You don't have to do anything "special" to ensure that you're using php -- any regular HTML can be changed harmlessly to the .php extension without a problem. However, if you are using FrontPage or Dreamweaver, you may find that they do not parse .php files. If that's the case, then you need to change to the .php extension after you have finished creating or editing your form. If you need to edit or change the file in the future, you can change it from .php to .html (or whatever file extension works properly in your HTML editor) and then back again without problem.

Placement of tags

We're going to use two tags in our main file. The first is the tag that asks the remote server what the IP address of the user is, while the second passes the information on to our processing file.

Both of the tags we're going to use are going to need to be anywhere inside the <form> </form> tags. Your "opening" form tag probably looks like this:

<FORM action=processor.php method=post>

Your closing form tag most likely looks like this:

</FORM>

Thus, as long as you place the tags we're going to talk about momentarily somewhere between these two form tags, it'll work.

First tag: get the info

As mentioned, our first tag gets the IP address from the remote server. You simply need to copy/paste this tag:

<?php $userip = $_SERVER['REMOTE_ADDR']; ?> 

As you can see, it is a php tag, thus the reason why we need the .php file extension. The part like so: $userip can be any tag. So you could use $foo if you wanted to, so long as it is not the same tag as any other you are using in this file. For the purposes of ease, it's probably best to keep it $userip, though.

Second tag: pass it on

Our next tag takes the information and passes it on to our processing file. Copy/paste this tag directly below the first tag:

<INPUT TYPE="hidden" name=formip value="<?php echo($userip); ?>">

As you can see by the "INPUT TYPE" part, this is a hidden tag. Which means that the user is not aware that it's there while viewing the page.

However, if the user opens the "page source" via their browser, they would see something like this (assuming the user's IP address is 10.10.10.10):

<INPUT TYPE="hidden" name=formip value="10.10.10.10">

If you want to show the user their IP address (for example, if you wanted some verbiage like "Your IP address is logged, and is as follows: 10.10.10.10"), then you would put the following tag anywhere you want the user to see their IP address:

<?php echo($userip); ?>

Enter the data into your e-mail

The final part of this is to show you the information you collected. Here, we're assuming that you're using some kind of template file to send yourself an e-mail of the output of the form the user has submitted.

So, passing on this information is just like displaying any other form field you've collected. Let's look at our "pass it on" tag again:

<INPUT TYPE="hidden" name=formip value="<?php echo($userip); ?>">

This is just like any other form value you're using, like a regular input box, that might look like this:

<INPUT size=33 name=email>

When you pass on the information from that tag, you use the name field, which would be (in this example) "email". On your template, you'd use $email as your tag.

Similarly, with our IP catching tag, the name is "formip", so on your template, you'd put $formip.

Test it out

You can test the process by following these steps:

  1. Set up your form, like we said, and upload it to your site.
  2. Now go to http://www.showmyip.com/. At the top, it'll show you the IP address of your computer.
  3. Now fill out your form and send it.
  4. When you check the form's output via e-mail, you should see the same IP address you saw on http://www.showmyip.com.