July 7, 2008

Limiting text input to characters and digits

To remove stuff such as punction and spaces (or anything you want) from a text box before sending to the server for validation you can use code like this, first the HTML:-

<input 
name="name" 
onblur="this.value = entrycheck(this.value);" 
type="text" 
size="20">

And then the following Javascript will remove invalid characters when the user moves to another field:-

<script type="text/javascript">
function namecheck(theInput) {
  var valid = 'abcdefghijklmnopqrstuvwxyz1234567890';
  var test ='';
  var ret ='';
  for(i=0;i<theInput.length;i++) {
    test = theInput.substr(i,1);
    if(valid.indexOf(test.toLowerCase()) != -1) {
      ret = ret + test;
    }
  }
  return ret;
}
</script>

In this example I just want digits and characters. If you want to add certain punctuation such as stops and commas just add them to the valid variable.

April 24, 2008

ASP code to block multiple ranges of IP’s from your site

It wasn’t long before I needed to expand upon banning a few IP’s. I needed to be able to ban whole networks so here’s the Q&D solution:-

First create a text file called ipsec.txt and enter some IP’s you wish to ban, to ban a network just leave off the end of the IP class number, for example:-

10.20.30.40
10.50

In the above example the first is a specific IP that you wish to ban, the second will ban all IP addresses that start 10.50 (for example 10.50.20.1 and 10.50.100.1 will both be banned). Enter as many as you like, one per line.

Now create or edit your global.asa file and add the following code to the session_onstart sub procedure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sub session_onstart
  remote_ip = request.servervariables("Remote_Addr")
  ipsec     = server.mappath("/ipsec.txt")
  ips       = getFileContents(ipsec)
  ips       = split(ips,vbcrlf)
 
  for each ip in ips
    if len(trim(ip)) &gt; 0 then
      ip_parts        = split(ip,".")
      remote_ip_parts = split(remote_ip,".")
      found = true
      for x = 0 to ubound(ip_parts)
        if ip_parts(x) <> remote_ip_parts(x) then
          found = false
          exit for
        end if
      next
      If found then
        response.redirect "/redir.html?ip=" & remote_ip
      end if
    end if
  Next
end sub

Replace /redir.html with a file or location you want to redirect banned ip’s too.

As this code runs in the session_onstart section of the global.asa it will be run only once for each visitor, this means that during the session they could return and this code would be bypased. If that is a concern modify the code to be a common function and place it strategically, perhaps in a common file called throughout the website.

April 22, 2008

Write ASP and need a way to block IP’s from your site?

This demonstrates how to ban a single IP address, later, I’ll show how to ban whole networks but chances are you will be able to work that out for yourself anyway after reading this anyway.

If global.asa does not exist then create it and add the following:

sub session_onstart
 
  ip = request.servervariables("Remote_Addr")
 
  select case ip
    case "111.111.111.111", "111.111.111.112", "111.111.111.123"
      response.redirect "http://www.example.com"
  end select
 
end sub

Replace 111.111.111.11x with the IP addresses you wish to ban, note they are comma separated and the last one is not followed with a comma.

You can change the redirection to a page on your site that informs them they are no longer allowed (a bit vindictive), to a blank page is probably best or you could even forward them on to a competitor :D

January 2, 2008

How The Brits Marshall Jets

Not entirely connected to software I have to agree, but my father, an ex RAF fighter pilot, sent this to me this morning and I just don’t want to lose it!

December 5, 2007

Software Engineering Tips For Startups

Boardroom Software Enginnering DiscussionsSeven years ago I tried to persuade my business partners and investors not to outsource our startup development to Oracle, but at the time ‘the city’ could not understand how two or three passionate programmers could compare with the might of a 20 strong team of Oracle professionals.

BlueBlog Alex article Software Engineering Tips For Startups provides an explanation of the issues the project suffered albeit unwittingly at the time.

The detachment of the programmers supplied by the outsourcing agency meant it was impossible for them to understand our passions and goals. They clocked in at 9.00am and were out by 5.00pm at the latest. Their conversation focused on TV, football and who was being promoted where in the company.

Their attitude was infectious and soon the original hand picked team was dissolusioned and faded into the background noise of the companies general operation.

But the company hasn’t failed. Seven years after funding it remains a veteran of the pre-bubble-bursting-funded-but-not -IPO’d companies. There’s not many of them left today. Their success, if you can call it that, has been due to two general business factors.

  1. The investors have put so much cash into the company they are not willing to let it go.
  2. Eliminating innovation and rationalising everything to a core product where they can rely on Investor relations to produce business.

It may pay the bills but it’s Hobsons Choice, they exist at the behest of the investor who provides cash and customers.

The only regret they have is not knowing where it would have gone had they been brave enough to give the original business a chance to develop.

They had an opportunity to launch the first community based business of it’s kind but that meant relying on a handful of super-geeks. Something institutional investors claimed they would never do.

Where are those investors now? They’re still there and they still have their hands on the controls, but they wobble from one investment to the next while Google, Yahoo and Microsoft cream off the best.