Categories
Uncategorized

Captcha’s in ASP.Net

I recently started getting some spam from my “contact us” page on my site.  I’d set it up “cleverly” so that it doesn’t try to send the mail to/from the addresses entered into the form since I’d seen online contact forms being used for “evil”.  Unfortunately I’d not taking into account the possibility that someone might use the form to send me spam.

Since I’d completely ignored that fact, I had not bothered to do any kind of captcha or other check to see that there it was a human that was sending the mails.  To be honest, part of the reason was that I’d imagined that making a Captcha work well would require WAY too much effort and I just wasn’t up to the task at the time.

Now that I was getting spam, I needed to make a plan and make one quickly before I was swamped by the mail.  Fortunately I remembered reading about ReCaptcha.  Basically they’re scanning books and words that are partly recognised get used in their captcha images.  Then when people enter the values for the captcha’s they’re helping ReCaptcha improve their OCR quality.

I headed on over to their site and was AMAZED at how many plugins and code “modules” they’d built.  Just take a look at the ReCaptcha Resources – When it comes to languages they have PHP, ASP.Net, Classic ASP, Python, Perl, Ruby, Java, JSP, and Coldfusion support.  And they have plugins for WordPress, MediaWiki, phpBB, Movable Type, Drupal, Symfony, Typo3, NucleusCMS, vBulletin, and Joomla.  That’s excluding the fact that they have an API that you could interact with directly.

On to the ASP.Net.  It’s REALLY easy.  You reference their DLL, then register a tag for their control on the page you want it to be placed on as follows:

<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

Then you sign up for an API Key, and place the Captcha control in place as follows:

<recaptcha:RecaptchaControl
  ID="recaptcha"
  runat="server"
  PublicKey=""           
  PrivateKey=""
  />

Then finally you just put in a bit of code on the submission of the form that checks:

if (Page.Valid())

If it’s not valid, then the user entered the wrong captcha.

Three simple steps, and super quick to implement… Now my Contact page has a Captcha on it :)  I love it when things just work, and work simply.

Categories
Development General

Java Dates from a n00b point of view

After spotting that seemingly 90% of the methods on the Date class were deprecated, I started getting worried.  Then when new Date().getYear() started giving me odd numbers like 109 instead of 2009.  I started thinking “Wow! Those Sun developers must have been on some strong drugs when they designed this language!”

So a bit more googling and some “WTF?!?!!?”’s later I found out that the Date class returns the time since January 1, 1970.  So of course .getYear() returns the number of years since then.  Well, not exactly.  Because there are only 39 years since 1 Jan 1970 and today.  So how the heck did getYear() return 109?  I didn’t bother to go any further than that – clearly the Sun developers were on drugs that should never have been invented, and I didn’t want to even think about starting to try and understand what was going on in their twisted little minds at that point in time.

BUT, I found out that they have a GregorianCalendar class which can help you out in most situations, even if it’s a little obtuse in how you use it.

The following code:

Calendar cal = new GregorianCalendar(mYear, mMonth, mDay, mHour, mMinute, mSecond);

Gives you a calendar object based on the input you gave it.  Now of course I get a Date object from a database so I want to do new GregorianCalendar(mDate).  But again, that would be asking too much, so instead of that simple instantiation I have to do:

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(creationDate);

Why, on earth did they call it setTime?  I mean, I’m setting a date and time.  I’m not just working with Time here folks! Surely the method should be named something a little better like .setDateTime() or .setDate()?

But wait… it gets better.  You know the Date class had a getYear() method, right? Remember? It’s the one that requires large amounts of hallucinogens to understand or use!  Well, naturally you’d expect that the Calendar class would have a getYear() that makes sense.

That’s where you’d be wrong!  Since they started with the obscure requirement of doing .GetInstance() followed by .setTime(mDate), they clearly hadn’t quite yet gotten off their high when they made the get() method.  Here’s how you use it:

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(creationDate);
mYear = cal.get(GregorianCalendar.YEAR);
mMonth = cal.get(GregorianCalendar.MONTH);
mDay = cal.get(GregorianCalendar.DAY_OF_MONTH);

Yup, that’s right… why make it as simple as cal.getYear() when you can make it cal.get(GregorianCalendar.YEAR)?  Perhaps someone over at Sun had some stock in a company that provides treatment for RSI’s, so that by making us type out loads more letters they hoped to make more of us suffer not only the pain of using their language, but also of repetitive stress injuries.

BIG DISCLAIMER:

From my brief work with Java previously, I know that the designers were NOT on any drugs and in fact they probably had very good reason for designing the classes the way that they did. It’s just not immediately obvious to the new user, and that’s mainly what I’m ranting about here.  So, to all the Java fanboys out there who are preparing to unleash their full fury on me for my sacrilegious comments about their language – please take a moment and laugh before you post. 🙂

Categories
Uncategorized

Vodacom, HTC Magic, and bad communication

HTCMagic_small I’m one of *those* geeks!  Yes, the ones who want the latest technology and become all obsessive about getting it.  The ones who make the lives of those around them miserable when waiting because they keep saying “When I get my X I’ll do Y” or “I just read that there’s now an awesome app for device X”.

Back in the day (4, or so years ago) I was a Vodacom customer, but I swapped to MTN to get the iMate SP3.  I then stuck with MTN at my next upgrade to get the HTC TyTn.  So when it came time for my upgrade this year, I looked around and found nothing really cool in South Africa.  But there was hope for the iPhone and with the possibility of one of our networks releasing an Android based phone.

Last month MTN released the HTC Dream, and there were rumours that Vodacom were going to release the HTC Magic.  Last week (on around the 12th of May) a number of local news sources reported that Vodacom was indeed going to be launching the HTC Magic and while most of them just repeated a press release, a few offered extra info like MyBroadband’s “Vodacom to launch Google G2 phone”.  They mentioned that it would be available next week (aka from the 18th of May).

Long story short, I spent over 3 hrs waiting on hold to speak to someone from Vodacom Direct only to find out that they knew nothing about the phone.  Neither did any of the 4 Vodashop’s that I tried to contact.  Everyone kept saying “It will be out mid June” despite the fact that there had been the press release and articles and even despite the fact that a Vodacom brochure outside each of the Vodashop’s had an ad for the phone saying “Launching May 2009”.

I was ready to write a really peeved post about how terrible Vodacom’s communications were (Did you know? They even got the phone number for Vodacom Direct wrong in their press release!) until a Vodacom representative (vodacom3g) replied to a post on the MyBroadband forums saying:

They are in the warehouse but we’re busy updating the firmware. (Otherwise you all bitch and moan again! )
So speak to your local shop (they all order their own stock). I’d venture it should be in the shops in the next few days.

[Link to post]

Soon after that they responded to a few other questions.  Suddenly I knew why there was a delay (although it didn’t explain why the various channels I’d tried earlier hadn’t known) and I was happy with the reason.

Hopefully the phones will have started shipping out to my local Vodashop in the next day or two and I’ll finally get my hands on one of these babies within the next week.

Categories
Development General

Android, Eclipse and Java – First Experiences

After bitching about the lack of good tutorials last time, I found that I’d actually been looking at Android 1.1 tutorials and trying to run them on Android 1.5.

Now that 1.5 is out (well, it was released quite a while ago, but I’ve only recently started using it in earnest) I’ve re-read the tutorials and some of the introductory info on the Android Developer’s Guide. In my exploring I’ve hit MANY “Force Close” dialogs.  Unfortunately they never seem to say anything about WHY the app is being forced to close.ForceClose

The first few times I’ve just popped Eclipse into debug mode in an attempt to figure out what’s wrong.  Unfortunately it doesn’t seem to help much.The exceptions that end up being thrown seem really vague and not entirely related to the issue at hand.  So without any further blabbing, here are my top three reasons for getting a “Force Close” in my Android development:

1. Putting the call to setContentView at the end of the onCreate method instead of the start.

Initially I thought I’d be clever and first setup all my event handlers, and data binding and stuff.  But after some really confusing exceptions I realised that unless I call setContentView at the top of my onCreate, I won’t have access to any of the “objects” on my form.

2. Having an abstact class instead of a concrete class for my Activity

I’m not entirely sure how Eclipse and I came to this point, but somehow when adding this class I must have ticked a box that I hadn’t ticked before and suddenly the class was abstract.  No idea how or why this happened, and I certainly didn’t notice it at the time.

Of course I’d expect the exception to say something about “You cannot create an instance of an abstract class.” or something  meaningful like that.  But no, it seemed that this was not to be.

3. Not adding the Activity to the AndroidManifest.xml

This happened at the same time as the issue in point two so I’m not too sure if the two issues together just made the cause of the problem really hard to find.   Maybe I’m just too used to Visual Studio protecting me from myself, but this seemed a little silly.  I created a class that extends Activity.  Now I still need to go and add that info to the AndroidManifest.xml file?  I

People might think that I’ve been overly coddled by VS.Net, and that this is simply how normal development works.  If I used a Wizard to credit an Invoice, I’d expect to have a credit note created at the end, ready to send on to the client.  I wouldn’t expect to have to then go and specify the Client, and add the information for the line items, and then add up the totals manually and then finally to add on the VAT before I could send it.

There are a few nice wizards to get things started, but they often seem to only go half way. If I use a wizard, I expect the wizard to do the whole job.  So when I add an Activity to an Android app, I’d expect it to create the class AND enter the information into the AndroidManifest.xml file for me. Actually, in this case there is really no Wizard in the SDK toolkit to add the Activity – and that’s another gripe – there are wizards to create an Android XML file which is relatively easy to do.  But something like creating a new Activity requires a bunch of manual steps.  So where’s the consistency?

Anyway – enough whining.  Soon I’ll face the anger of a mob of Java developers. 😉