Banning IPADDRESSES.
We will here be discussing how we can block IP Addresses Programtically Using Coldfusion. I am using MYSQL database Here. But you can also use MSSQL and Whatelse other database you want to.
So let's get started with our script now. we will be doing all in steps and i will describe everything in step by step way. So here we have our
Setp1: Create a Database table with the Following columns as:
CREATE TABLE 'bans' (
'ban_id' int(11) NOT NULL auto_increment,
'ban_uname' varchar(255) default NULL,
'ban_ip' varchar(255) default NULL,
PRIMARY KEY ('ban_id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The Table is Pretty self-Explanationary, so no need to explain what exactly we have added here. Now moving forward we will move to step 2:
Step we will create a Page called Banner.cfm
<cfinvoke component="#request.cfcPath#.bans" method="getbanned" returnvariable="Getbans"/>
we invoke the above getbans table which will fetch all the records from the bans table: the query it fetches is like this:
<cffunction access="public" name="getbanned" returntype="query">
<cfset var mybans = "">
<CFQUERY datasource="#request.dsn#" name="mybans" username="#request.user#" password="#request.pass#">
SELECT * FROM bans
</CFQUERY>
<cfreturn mybans>
</cffunction>
<TABLE border="0" cellpadding="2" cellspacing="2" width="100%">
<TR>
<TD align="center" class="oncaption"><strong>Forbidden IPs</strong></TD>
</TR>
<cfif isDefined('n')>
<tr><td colspan="2" align="center" class="MainBar"><cfoutput>#n#</cfoutput></td></tr>
</cfif>
</TABLE>
<TABLE border="0" cellpadding="2" cellspacing="2" width="100%">
<TR>
<cfform action="#cgi.SCRIPT_NAME#?#cgi.QUERY_STRING#" method="post">
<TD align="center" class="txt"><strong>Add Forbidden IP Address:</strong>
<input type="text" name="IP" size="1">
.
<input type="text" name="IP" size="1">
.
<input type="text" name="IP" size="1">
.
<input type="text" name="IP" size="1">
<input name="Add" type="submit" class="border" value="Add"></TD>
</cfform>
</TR>
<TR>
<td align="left"><cfif GetBans.recordcount gt 0>
<table width="50%" border="0" cellspacing="1" align="center" class="pClrBody">
<tr>
<td height="20" colspan="2"></td>
</tr>
<tr>
<td align="right" valign="top" class="txt">IP Address List</td>
<td><cfform action="#cgi.SCRIPT_NAME#?#cgi.QUERY_STRING#" method="post">
<select name="RemoveIP" size="10" multiple>
<cfoutput query="getbans">
<option value="#ban_ip#">#ban_ip#
</cfoutput>
</select>
<input name="Remove" type="submit" class="border" value="Remove">
</cfform></td>
</tr>
</table>
<cfelse>
<TABLE border="0" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td height="100"></td>
</tr>
<TR>
<td align="center" class="txt">There are no Forbidden IP Address.</td>
</TR>
</TABLE>
</cfif></td>
</TR>
<tr>
<td height="50"></td>
</tr>
</TABLE>
The Page is Pretty Self-Explained. What we are doing here is we are taking 4 textboxes and name them same with a size of 1 only all four textboxes appear by dot after each textbox. so this shows we can add the ipaddress as 198.168.21.33 in the textbox and that will the add button which will ad the ipaddress to the table:
Here how it will do:
<cfif isdefined ("form.Add")>
<cfif listlen(form.ip) LT "4">
<cfset looplen = 4 - listlen(form.ip)>
<cfloop from="1" to="#looplen#" index="B">
<cfset form.ip = listappend(form.ip, "*")>
</cfloop>
</cfif>
<cfset form.ips = rereplace(form.ip, ",", "." , "ALL")>
<cfinvoke component="#request.cfcPath#.bans" method="addban" ips="#form.ips#"/>
<cfset n = "Cool! Ban IP has been added Successfully">
</cfif>
So component will act as following:
<cffunction access="public" name="addban" returntype="void">
<cfargument name="ips" default="" required="no">
<cfset var mybans = "">
<cfset var mycheck = "">
<CFQUERY datasource="#request.dsn#" name="mycheck" username="#request.user#" password="#request.pass#">
SELECT * FROM bans WHERE
ban_ip = <cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(arguments.ips)#">
</CFQUERY>
<cfif mycheck.recordcount>
<cfelse>
<CFQUERY datasource="#request.dsn#" name="mybans" username="#request.user#" password="#request.pass#">
INSERT INTO bans (ban_IP) VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.ips#">)
</CFQUERY>
</cfif>
</cffunction>
After the same form add Button, we have provided a Select Box with option of Multiple set to Yes. it queries the records from the bans table which will list all the banned IPaddresseslist in the selectbox. clicking on multiple items in a select box or 1 item in a select box will execute the delete query:
Here how it will do:
<cfif isdefined ("form.Remove")>
<cfinvoke component="#request.cfcPath#.video" method="RemoveBans" returnvariable="str" structform="#form#"/>
<cfset n = "Cool! Ban IP has been removed Successfully">
</cfif>
The Invoke Functionality of the remove IPADDress Command
<cffunction access="public" name="removebans" returntype="void">
<cfargument name="r" default="" required="no">
<cfset var mybans = "">
<cfloop from="1" to="#ListLen(arguments.structform.removeip)#" index="r">
<CFQUERY datasource="#request.dsn#" name="mybans" username="#request.user#" password="#request.pass#">
DELETE From bans
WHERE
ban_ip = '#ListGetAt(arguments.structform.removeip, r)#'
</CFQUERY>
</cfloop>
</cffunction>
That above was the backend way, how we manage the same at the front end, we can do something like this:
We will create a Common.cfm page which we can add to the Application.cfm or Application.cfc file which will get executed with every page request. it will check if the certain ipaddress accessing the website is banned or not: heere is how we will make the check:
<cfinvoke component="#request.cfcPath#.bans" method="getbanned" returnvariable="Getbans"/>
<Cfloop query="getbans">
<cfif getbans.ban_ip contains "*">
<cfset Checkip = rereplace(getbans.ban_ip, ".\*", "", "ALL")>
<cfset newposition = listlen(Checkip, ".")>
<cfset statusOfip = "">
<cfloop from="1" to="#newposition#" index="I">
<cfset StatusOfip = listappend(StatusOfip, listgetat(cgi.REMOTE_ADDR, I, "."), ".")>
</cfloop>
<cfif Checkip is statusOfip>
<p align="center"><strong>You have been banned!</strong></p>
<cfabort>
</cfif>
<cfelse>
<cfif getbans.ban_ip is cgi.REMOTE_ADDR>
<p align="center"><strong>You have been banned</strong></p>
<cfabort>
</cfif>
</cfif>
</CFLOOP>
That's All. A Complete Banning System You Love to See on Your Site. Next time I will show you how to ban the Version 6 Ipaddress.
Cheers