This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Saving IP address & country name in emails


  1. DavidS
    Member

    Hi there,

    I know it's possible to save the IP address in the emails sent from the contact form. But I'm wanting to go one step further. I'd like to automatically translate that IP address so that it actually displays the country name associated with that IP.

    I found excellent code to do what I want here: http://phpweby.com/software/ip2country
    But I don't know if this can easily be incorporated into MachForm.

    Ideally, I would like to have something similar to the following included in the emails:

    Country name: United States
    Country code: US
    IP address: 67.209.225.167
    IP number: 1137828263

    Any ideas?

    Thank very much,

    David

    Posted 14 years ago #
  2. redityo

    Hi David,

    You need to edit "includes/helper-functions.php" file for this. First of all, you need to download all required library. Try to follow these steps :

    1. Create "geoip" folder inside your machform folder.
    2. Download the files in here :

    http://www.maxmind.com/download/geoip/api/php/

    http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

    3. Extract "GeoIP.dat.gz"
    4. Put all files into "geoip" folder.

    5. Edit machform "includes/helper-functions.php" file and go to around line 461 for these code :

    $email_content = str_replace($template_variables,$template_values,$content);

    then put these code exactly bellow that line

    if ($target_is_admin === true) {
    	$email_content .= "<br /><br />";
    	//geo ip
    	require("geoip/geoip.inc");
    
    	$gi = geoip_open("geoip/GeoIP.dat",GEOIP_STANDARD);
    
    	$email_content .= "IP ADDRESS : " . $ip_address . "<br />";
    	$email_content .= "Country Code : " . geoip_country_code_by_addr($gi, $ip_address )  . "<br />";
    	$email_content .= "Country Name : " . geoip_country_name_by_addr($gi, $ip_address ) . "<br />";
    
    	geoip_close($gi);
    
    }

    That would attach more information about the ip address on mail notifiction


    MachForm Support

    Posted 14 years ago #
  3. DavidS
    Member

    Hi Redityo,

    That's FANTASTIC! Works great. Thank you for the help.

    Would it also be possible though to have the same information collected and displayed in the "Entries" listing from the Form Manager screen? If possible, I'd like to save that data and be able to export to a .csv file.

    Thanks for the help,

    David

    Posted 14 years ago #
  4. redityo

    Sounds great!
    To put the data on your entries page, you need to use different way. To do this, first of all you need 3 single line text filed into your form to hold the ip country data. Let say you have those new 3 fields on form 11 with fields id 1,2,3. Then you can follow to these steps :

    1. Put this code into "view.php" and "embed.php"

    require("geoip/geoip.inc");

    2. Edit "includes/view-functions.php" file and go to around line 1682 for this code :

    $element[$j]->default_value = htmlspecialchars($row['element_default_value']);

    then put these code bellow that line

    if ($form_id ==  11 && ($element[$j]->id == 1 || $element[$j]->id == 2 || $element[$j]->id == 3)) {
    
    	//geo ip
    
    	$gi = geoip_open("geoip/GeoIP.dat",GEOIP_STANDARD);
    	$ip_address = $_SERVER['REMOTE_ADDR'];
    	if ($element[$j]->id == 1) {
    		$element[$j]->default_value = $ip_address;
    	} elseif ($element[$j]->id == 2) {
    		$element[$j]->default_value = geoip_country_code_by_addr($gi, $ip_address );
    	} elseif ($element[$j]->id == 3) {
    		$element[$j]->default_value = geoip_country_name_by_addr($gi, $ip_address );
    	}
    
    	geoip_close($gi);
    
    }

    3. Add these css code into your form css :

    #li_1,#li_2,#li_3 {
    display:none !important;
    }

    Don't forget to change those id's with yours.


    MachForm Support

    Posted 14 years ago #
  5. DavidS
    Member

    Hi Redityo,

    Thank you for all these details, but one point I do not understand is why is there an IP Address written into the code for "includes/view-functions.php" file? [$ip_address = "24.24.24.24";].

    The whole reason for doing all of this is to obtain the IP Address of the person submitting the form.

    Other than that, I've been able to modify the codes and the entries page is displaying the lines properly, just not with the proper data of the submitter.

    Thank you again for the help,

    David

    Posted 14 years ago #
  6. DavidS
    Member

    Sorry, another question regarding this feature.

    When the user fills out the form, the fields for IP Address, Country Code, and Country Name are indeed hidden from display. However, I have my form setup so the user can review the data before submitting. At this point, those 3 fields I just mentioned are visible. Is it possible to hide them from the review page as well?

    Thanks Redityo,

    David

    Posted 14 years ago #
  7. redityo

    Sorry David, the ip address come from my test code :)
    I've update above code, you can refer to the latest one. And to hide the entry value on review page, you can edit "view-functions.php" file and go to around line 2349 for these code :

    foreach ($entry_details as $data){

    then put these code bellow that line

    if ($form_id ==  11 && ($data['element_id'] == 1 || $data['element_id'] == 2 || $data['element_id'] == 3)) {
    	continue;
    }

    MachForm Support

    Posted 14 years ago #

RSS feed for this topic

Reply