Using GeoPlugin PHP API to Get Country Name and Country IP address.
Some time in HTML contact or WordPress contact form 7 we need to Country and Country IP address where the user fills the form. It’s required for tracking which country user used the form and find out where a lead comes from and which country user using our website lead form. So that requires storing Country name and country IP address in the form of hidden fields. The below code shows how to do this task.
PHP Code for getting Country Name and Country IP Address by using Geoplugin PHP API.
<?php if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } $data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip)); $ClientState = $data['geoplugin_countryName']; $ClientIPAddress = $data['geoplugin_request']; ?>
jQuery Code for placed or store values in hidden form fields.
<script type="text/javascript"> jQuery(document).ready(function(){ jQuery('#countryIPGrow').val('<?php echo $ClientIPAddress;?>'); jQuery('#countryLocationGrow').val('<?php echo $ClientState;?>'); }); </script>
HTML Fields for creating Hidden fields in the contact form.
<input type="hidden" name="countryIPGrow" id="countryIPGrow" value=""> <input type="hidden" name="countryLocationGrow" id="countryLocationGrow" value="">
WordPress Contact Form 7 code for creating hidden fields if using WordPress.
[hidden countryIP id:countryIPGrow class:countryIPGrow] [hidden countryLocation id:countryLocationGrow class:countryLocationGrow]