Back

IP Address Restrictor

Description

This is code so that you can change the page based on the incoming IP Address - for example, we use it to:

A) Make certain information only available to those on campus (a staff directory)

B) We have various online databases through our library that access easily on site, but if you are offsite, you need to go through a proxy. This allows one page to have the 'correct' links either way. 

 

Code

###IP ACCESS Restrictor
##MP 2012 June
##NOTE extra information left in comments so you can see if it you like


#set ($ip=$request.getRemoteAddr())
#set ($fp=$ip.substring(0,$ip.indexOf('.',0)))

#set ($fd=$ip.indexOf('.',0))
#set ($fdp=$fd + 1)
#set ($sd=$ip.indexOf('.',$fdp))
#set ($sd=$sd + 1)

#set ($sp=$ip.substring($fdp,$ip.indexOf('.',$fdp)))
#set ($tp=$ip.substring($sd,$ip.indexOf('.',$sd)))


##<p> IP : $ip </p>
##<p> FD : $fd </p>
##<p> FD : $fdp </p>
##<p> Firstpair : $fp </p>
##<p> Secondpair : $sp </p>
##<p> Thirdpair : $tp </p>

#set ($pass = "FALSE")


##allow all 10. x. x .x addresses
#if ($fp == 10)
#set ($pass = "TRUE")
#end

##allow all 192.159.104. x addresses
#if ($fp == 192 && $sp == 159 && $tp == 104)
#set ($pass = "TRUE")
#end

##allow all 64.9.76. x addresses
#if ($fp == 64 && $sp == 9 && $tp == 76 )
#set ($pass = "TRUE")
#end


#if ($pass == "TRUE")
You are on campus.
#end

#if ($pass == "FALSE")
You are off campus. 
#end