All Collections
Technical Stuff
Help Articles
Appending IP Address to a Form with PHP [For Advanced Users]
Appending IP Address to a Form with PHP [For Advanced Users]

For any PHP/Wordpress developers hoping to capture an IP address in a form.

Support Team avatar
Written by Support Team
Updated over a week ago

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 BenchmarkONE 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:


       
        ';
        ?>
       
       
       
       
       
        Submit
 


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. 

Did this answer your question?