Replaced ReCaptcha with Akismet

  • Digg this article
  • Sphinn this article
  • Stumble this article
  • Facebook
  • del.icio.us
  • LinkedIn
  • Twit this article
Posted by John
on Monday, 05 January 2009

Spent a little bit of time over the weekend replacing the ReCaptcha anti-spamming filter I was using in Matilda with Akismet mainly because it's a little less in-your-face to the user.

Don't get me wrong, ReCaptcha was good and stopped comment spam 100% in it's tracks but the comments I got back and what I felt about it was that it was a bottleneck in the UI; and it needed to go simply to be easier for the poster.

So some quick research on the subject produced two possibilities, build my own Akismet handler or pop in JFrench's RAkismet plugin, the later being the quicker idea.

All it requires you to have is a Akismet key that can be obtained freely thru a Wordpress.com account. Put that along with you're site's URL into your rakismet.rb file, make the changes to your relevant models & controller and you're away.

So install with;

script/plugin install git://github.com/jfrench/rakismet.git

Next add it to the model's that you'll need akismet against and tell it what fields you've got for it to filter with (in case it's spam);

class Comment < ActiveRecord::Base

has_rakismet  :author => :username,
            :author_url => :url,
            :author_email => :email,
            :user_ip => :ip_address,
            :user_agent => :user_agent,
            :referrer => :referrer

Add the handlers only for the controller actions you need;

class BlogController < ApplicationController
  has_rakismet :only => [:post_comment, :post_message]

This will give you a simple .spam? method you can then call on you're new model object to check whether it's spam. On top of this add to your model extra fields to store info about the user's ip, agent and referrer which you can feed into Akismet and it's filter above to improve your chances of filtering out the bad.

request.env['REMOTE_HOST'] #ip_address
request.env['USER_AGENT'] #user_agent
request.env['HTTP_REFERER'] #referrer

Put all the changes into my Matilda CMS's GitHub repository, do need to add some buttons to clear spam quickly and mark what's ham; but all in time.

Request.QueryString + Select Redirect

  • Digg this article
  • Sphinn this article
  • Stumble this article
  • Facebook
  • del.icio.us
  • LinkedIn
  • Twit this article
Posted by John
on Thursday, 14 February 2008

Quick post, to handle parameters in your webpage via javascript, try this...

Request.QueryString with JS

Include the file in your header section then use like...

mypage.html?username=john

With the Javascript...

var name = '';
if (Request.QueryString("username").Count > 0) {
    name = Request.QueryString("username").Item(1);
}

The function returns the querystring as an object, so to determine whether you have any parameters returned count the number of objects. The first of which we want so we use .Item(1) to grab that and stick it in our variable name after which we can give to an alert() popup or whatever we need.

On Select Redirect with JS

Here's another bit, this will force the browser to redirect to another page once the user selects a value from the drop-down box and clicks the button image.

Javascript...

function goto(form) {
var index=form.select.selectedIndex
    if (form.select.options[index].value.length != 0) {
        location.replace(form.select.options[index].value);
    }
}

Html...

<FORM NAME="form1">
<select name="select" id="select" style="width:90px;">
<option value="http://www.red91.com/">my site</option>
</select>
<img src="button.gif" onclick="goto(form1)" />
</FORM>

AJAX

  • Digg this article
  • Sphinn this article
  • Stumble this article
  • Facebook
  • del.icio.us
  • LinkedIn
  • Twit this article
Posted by John
on Saturday, 04 August 2007

AJAX = Asynchronous JavaScript And Xml

Based on Javascript and HTTP Requests (more importantly the XmlHttpRequest).

Code

function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
// ..IE / Opera ..
}
xmlHttp.open("GET","time.asp",true);
xmlHttp.send(null);
}

First create a variable to store your returned data, xmlHttp, create your new XMLHttpRequest() object.

The two lines at the bottom fire the GET request and the time.asp code for a value, true tell it's to treat this as asynchronous (e.g. don't wait around for the answer). The final line actually sends the request.

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4) {
// handle returned data
document.myForm.time.value=xmlHttp.responseText;
}
}

The .responseText stores the data and is used to change the time value in the form myForm of our html document.

states: 0 = request not initialised 1 = request setup 2 = request sent 3 = in progress * 4 = request complete

The Calling Object

We could use .NET, Java or PHP, but to keep this example really simple here's the calling file time.asp

<%
response.expires=-1
response.write(time)
%>

This simply sets the object expiry period and responds to the request, sending back a value.

How do we fire it?

Really simple, just tie the _ajaxFunction() to an event within your html document, like so...

<input type="text" +onkeyup="ajaxFunction();" name="username" />

So when the user key's up (moves away from the username textbox) the ajaxFunction() will be fired.

Other Events

onblur = element loses focus (clicks away)
onchange = content changes (user types in box)
onfocus = element gets focus (clicks box / button)
onload = page or image is finished loading
onselect = text is selected
onsubmit = submit button is clicked, form data submitted)