RBA Cash Rate: 4.35% · 1AUD = 0.71 USD · Inflation: 4.6%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.24% (5.24%*) • Home Loan Fixed: 5.70% (6.06%*) • Fixed: 5.70% (6.06%*) • Variable: 5.24% (5.24%*) • Investment IO: 5.69% (5.72%*) • Investment PI: 5.49% (5.52%*)

Get the IP Address of a Visitor with PHP (or WordPress Shortcode)

There are hundreds of functions found over the web that can be used to find the real IP address of a visitor. Here's one function that works well for us. The function searches environmental variables, validates a likely IP against PHP5's filter_var() function and then returns the value.

1
<?php 
2
/*
3
 Get the IP Address of a Visitor with PHP
4
 http://www.beliefmedia.com/get-ip-address
5
*/
6
 
7
function beliefmedia_ip() {
8
 foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
9
     if (array_key_exists($key, $_SERVER) === true) {
10
         foreach (explode(',', $_SERVER[$key]) as $ip) {
11
            if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
12
            return $ip;
13
          }
14
        }
15
     }
16
  }
17
}
18
 
19
/* Usage */
20

As with all IP-based (or HTTP header) tools, it's easy to spoof. For the majority of purposes, though, it serves its purpose nicely.

Display a User IP Address with WordPress Shortcode

For the sake of it, here's how you would display the IP of a visitor in a WordPress post or page with very simple shortcode.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Get the IP Address of a Visitor with PHP
4
 http://www.beliefmedia.com/get-ip-address
5
*/
6
 
7
function beliefmedia_ip() {
8
 foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
9
     if (array_key_exists($key, $_SERVER) === true) {
10
         foreach (explode(',', $_SERVER[$key]) as $ip) {
11
            if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
12
            return $ip;
13
          }
14
        }
15
     }
16
  }
17
}
18
add_shortcode('getip','beliefmedia_ip');

You may optionally download the WordPress plugin. Render the IP address of a user with [getip].

Usage:

The shortcode of [getip] would return 216.73.217.128 (your IP address).

Applications

If you wanted to expand upon the function, you could add geo-specific services to users, add maps, use custom geographically targeted titles, render custom advertising, display a geographic user plot, or deliver geographically appropriate content (such as local weather) - all of which we'll be sharing in the future (it's the purpose of sharing this simple function).

Download


Title: Get the IP Address of a Visitor with PHP
Description: Return the IP address of a user with PHP or WordPress shortcode (WP Shortcode).
  Download • Version 0.1, 517.0B, zip, Category: WordPress Shortcodes
WordPress Plugins (General), (800.0B)    PHP Code & Snippets, (501.0B)    

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
Timezone: E. AUSTRALIA STANDARD TIME · [ CHANGE ]

Related Articles:

John Snow Cholera Map, 1854

The Xena Statistical Engine

The level of statistical integration availed to those that use Yabber (and our broker website) was always industry leading. However, with over 100 modules that

Read More »
RBA Building, Sydney, 6 February 1963

The RBA API

The Reserve Bank of Australia (RBA) publishes large amounts of data to its website as Excel Spreadsheets, but this method makes advanced queries and analytics

Read More »

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest
      REVIEW