Form User Geographical Location
Your forms can tell you the geographical location of your form user, based on their IP address.
The information is good to know. Cultural differences may affect communication, which can be taken into account in case of misunderstandings. Time zones may affect response time, which may mellow expectations. Knowing where someone is located may establish some affinity.
You'll need to insert PHP code into your form to include the IP address information. It inserts the information into your form with hidden fields.
If you receive an email when people use your form, the IP address information should now also be included. Similarly, if form content is stored in a database.
In the unlikely event that the IP address information is not included in the email you receive or stored in the database with the rest of the form information, then whatever you normally do in conjunction with adding any field to a form to make the software process the added field is what you do in conjunction with adding the IP address information fields. (If you're using our software and you have a question about that, support is available.)
The geolocation information is provided by http://freegeoip.net/. The freegeoip.net site will accept up to 15,000 requests for IP address information per hour from your server. (If your form loads more often than 15,000 times per hour, freegeoip.net has a button where you can download the software, for free, so you can run it on your own server.)
The geolocation values inserted into the hidden fields are:
- IP — the IP address of the form user.
- Country — the geographical country name.
- Region — the region within the country.
- City — the city within the region.
- Zip — the zip/postal code for the city.
Please note that IP information isn't always accurate. Also, certain values aren't always available (in which instances the corresponding values will be blank).
When you put the PHP code into the form, the PHP code will insert IP address information. It will insert either a separate hidden field for each geolocation value or one hidden field for all the information.
Here is an example of the separate hidden fields that the PHP code will insert into your form.
<input type="hidden" name="__IP__" value="173.254.216.66"> <input type="hidden" name="__Country__" value="United States"> <input type="hidden" name="__Region__" value="California"> <input type="hidden" name="__City__" value="Los Angeles"> <input type="hidden" name="__Zip__" value="90014">
When the PHP code inserts all the IP address geolocation values as one hidden field (example below), each geolocation name and value is separated with a colon character (as in Region:California
). And the name/value sets are separated with a semi-colon character (as in Region:California;City:Los Angeles
).
Here's an example.
<input type="hidden" name="__ipinfo__" value="IP:173.254.216.66;Country:United States;Region:California;City:Los Angeles;Zip:90014">
The PHP Code
The PHP code is inserted into your form, somewhere between the opening <form …>
tag and the closing </form>
tag. The only required customization is where you specify whether you want one hidden field with all the IP address geolocation information or a separate hidden field for each.
Here is the PHP code. Notes follow.
<?php $AllInformationInOneField = true; $__data__ = json_decode(file_get_contents("http://freegeoip.net/json/{$_SERVER['REMOTE_ADDR']}")); if( empty($AllInformationInOneField) ) { echo <<<FIELDS <input type="hidden" name="__IP__" value="{$__data__->ip}"> <input type="hidden" name="__Country__" value="{$__data__->country_name}"> <input type="hidden" name="__Region__" value="{$__data__->region_name}"> <input type="hidden" name="__City__" value="{$__data__->city}"> <input type="hidden" name="__Zip__" value="{$__data__->zip_code}"> FIELDS; } else { echo <<<FIELD <input type="hidden" name="__ipinfo__" value="IP:{$__data__->ip}:Country:{$__data__->country_name}:Region:{$__data__->region_name}:City:{$__data__->city}:Zip:{$__data__->zip_code}"> FIELD; } ?>
Notes:
The only place that must be customized is where you specify if you want all IP address information in one hidden field or in separate hidden fields. It's near the top of the above PHP code.
Specify true (no quotes) for all information in one field. Otherwise, specify false (also no quotes).
Here's how to specify one hidden field for all the IP address information.
$AllInformationInOneField = true
;
And this is how to specify that each geolocation item be in a separate hidden field.
$AllInformationInOneField = false
;
Other code that may be changed in the PHP code are the hidden field names. The current names are colored red.
The group of hidden fields near the middle of the PHP code is to assign geolocation values to separate fields:
<input type="hidden" name="__IP__" value="{$__data__->ip}"> <input type="hidden" name="__Country__" value="{$__data__->country_name}"> <input type="hidden" name="__Region__" value="{$__data__->region_name}"> <input type="hidden" name="__City__" value="{$__data__->city}"> <input type="hidden" name="__Zip__" value="{$__data__->zip_code}">
If needed, change those red field names for your implementation.
The name="__ipinfo__
", near the bottom of the PHP code is for the field containing all the geolocation information:
<input type="hidden" name="__ipinfo__" value="IP:{$__data__->ip}:Country:{$__data__->country_name}:Region:{$__data__->region_name}:City:{$__data__->city}:Zip:{$__data__->zip_code}">
If needed, change the field name __ipinfo__
to the field name required for your implementation.
Getting it Going
Put the PHP code into your form and give it a try.
After the web page with the form loads at your domain, view the web page source code to see the hidden fields that the PHP code inserted. The hidden field values will be the geolocation information of your own IP address (because you are the one who loaded the form).
Submit the form. Verify your software handles the hidden field data correctly. And then you're good to go.
(This article first appeared in Possibilities newsletter.)
Will Bontrager