Control modules by IP address

(5 votes, average 3.20 out of 5)

In company intranet systems you may be able to identify certain groups of users by the IP address of the client. These MetaMod recipes allow you to to detect an individual or range of IP addresses, and change modules based on this information.

With JomGenius

This technique is available from MetaMod 2.4 onwards.

The core_genius "client_ip" check attempts to check if the connection is being made via a proxy server. If the proxy server supports it, core_genius is then able to retrieve the original IP address of the connection. Note that this additional check means it can be spoofed. If this matters to you, use the non-JomGenius technique instead of this one.

Single IP address check

if ( $core_genius->check( "client_ip = 123.22.33.44" ) ) return 56;

IP address range example 123.22.33.50-55

// split ip address into numbers, we test individually
$ip_numbers = explode( ".", $core_genius->info( "client_ip" ) );
if ($ip_numbers[0] == 123 &&
 $ip_numbers[1] == 22 &&
 $ip_numbers[2] == 33 &&
 $ip_numbers[3] >= 50 &&
 $ip_numbers[3] <= 55) return 12;

Pre-JomGenius

If you have MetaMod 2.4 or more recent, try the "JomGenius" version instead of this one.

Single IP address check

if ($_SERVER['REMOTE_ADDR'] == "123.22.33.44") return 56;

IP address range example 123.22.33.50-55

// split ip address into numbers, we test individually
$ip_numbers = explode(".",$_SERVER['REMOTE_ADDR']);
if ($ip_numbers[0] == 123 &&
 $ip_numbers[1] == 22 &&
 $ip_numbers[2] == 33 &&
 $ip_numbers[3] >= 50 &&
 $ip_numbers[3] <= 55) return 12;