For any PHP/Wordpress developers hoping to capture an IP address in a form (for tracking or Google Analytics purposes) you're in luck. PHP allows you to parse headers using the $_SERVER array, which is automatically populated by your web server. 


There is a bit of setup required. You will need to create a custom field in Hatchbuck which will house the data you're intending on capturing. Make sure it's a single-line text field, not numeric. Next, you will need to implement the code on your page with one of the embed options that allow you access to the source code, i.e. NOT the iframe. This will allow you to make the input hidden and inject the IP address as the value of the field. 


The following function will allow you to pull the IP address of a web visit's device (in most cases). Keep in mind that HTTP headers are not standardized, which is why we need the conditional statements:

  

function getIP() {
        $client  = @$_SERVER['HTTP_CLIENT_IP'];
        $forwarded = @$_SERVER['HTTP_X_FORWARDED_FOR'];
        $remote  = $_SERVER['REMOTE_ADDR'];

        if (filter_var($client, FILTER_VALIDATE_IP)) {
                return $client;}
        elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
                return $forward;}
        else {
                return $remote;}
} 

  

You can simply call the function, assign it to a variable, and insert the IP as the value of a hidden field:

        

<form action="https://app.hatchbuck.com/onlineForm/submit.php" method="post" name="form_999999123458">
        <input name="q4_email" type="email" placeholder="Email Address" />
        <?php
	        # Calling function to populate the variable, then creating a hidden input and populating it
	        $ip = getIP();
	        echo '<input name="whatever_hatchbuck_generates" type="hidden" value="' . $ip . '" />';
        ?>
        <!-- Hatchbuck boilerplate -->
        <input type="hidden" name="formID" value="12345678910">
        <input type="hidden" name="enableServerValidation" value="1">
        <input type="hidden" name="enable303Redirect" value="0">
        <input type="hidden" id="simple_spc" name="simple_spc" value="12345678910-12345678910">
        <button type="submit">Submit</button>
</form> 

        


If you're using Wordpress, you can put the function into your functions.php file. If you're using a custom page, you can put the function in any included/required scripts, or even on the same page. 


Note: Support cannot assist with implementing or troubleshooting this. All code examples may need to be modified to work within an individual website, and they are intended for advanced users only.