With this snippet you can allow to view given forums for given subnets only
Requested by: Knopper
Request topic: http://www.phpbb.com/community/viewtopi ... &t=2157326
Screens:
User IP not in subnet
User IP in subnet
Instruction
open
viewtopic.php
find
- Code: Select all
// Output the page
before add
- Code: Select all
// Some forums allowed for given subnets only / 4seven / 2012
### CONFIG
// Put here the forum_ids, which will
// be restricted for given ip subnets,
// eg. array(16,35,12,6);
$forum_id_arr = array(4,5);
### CONFIG
// no forum, no action
if (isset($forum_id) && ($forum_id !== 0)){
// only, if forum is in array
if (in_array($forum_id, $forum_id_arr)){
// Thanks to Bill Grady
// and zend.framework
function ip_in_subnet($ip) {
### CONFIG
// put in the subnet range (cidr),
// eg. array("24.256.0.0/20");
// enter more ranges in this way
/* array("24.45.0.0/08",
"46.73.0.0/24",
"54.25.0.0/20");
*/
$source = array("56.41.144.0/20");
### CONFIG
// if array or not, pass through
foreach ($source as $line) {
// Get the base and the bits from the CIDR
list($base, $bits) = explode('/', $line);
// Now split it up into it's classes
list($a, $b, $c, $d) = explode('.', $base);
// Now do some bit shifting/switching to convert to ints
$i = ($a << 24) + ($b << 16) + ( $c << 8 ) + $d;
$mask = $bits == 0 ? 0: (~0 << (32 - $bits));
// Here's our lowest int
$low = $i & $mask;
// Here's our highest int
$high = $i | (~$mask & 0xFFFFFFFF);
// Now split the ip we're checking against up into classes
list($a, $b, $c, $d) = explode('.', $ip);
// Now convert the ip we're checking against to an int
$check = ($a << 24) + ($b << 16) + ( $c << 8 ) + $d;
// If the ip is within the range, including highest/lowest values,
// then it's witin the CIDR range
if ($check >= $low && $check <= $high) {
return true;
}
}
return false;
}
// getting current ip address
if(!isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$client_ip = $_SERVER['REMOTE_ADDR'];
}
else
{
$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// check, if ip is in subnet
if(!ip_in_subnet($client_ip))
{
// trigger error / no access
trigger_error('Sorry, no access to this forum from your network.
<br /><a href="javascript:history.back()">Back to last site</a>');
}
}}
// Some forums allowed for given subnets only / 4seven / 2012
- Make settings in both
### CONFIG
Sections - Read comments