Web

4th

Sep

Math Guard

This will help you implement Math Guard 2.0. in Joomla’s contact form, step-by-step.

First, download the zip file from Code Gravity. Extract the zip file to a temporary folder anywhere on your harddrive. Preferably, a place that you will remember.

1. Now, open up ClassMathGuard.php in a text editor or a html editor of your choice and add the following line to it, at the top, just underneath <?php

// no direct access
defined( ‘_VALID_MOS’ ) or die( ‘Restricted access’ );

this will ensure that no-one has access to this file outside of Joomla.

Then, upload that file – only that file – to your /components/com_contact/ directory. FYI – you don’t need the rest, part of Math Guard’s formhandler.php is inserted into contact.php later on in this tutorial.

2. Find the contact component (/components/com_contact/) in your joomla installation and open contact.html.php

Find:

<?php
<if ( $params->get( 'email_copy' ) ) {
?>
<br />
<input type="checkbox" name="email_copy" id="contact_email_copy" value="1" />
<label for="contact_email_copy">
<?php echo(_EMAIL_A_COPY); ?>
</label>
<?php
}
?>
<br />
<br />

and between the 2 break tags at the end (<br />) insert the following:

<?php require("ClassMathGuard.php"); MathGuard::insertQuestion(); ?>

Of course, you can add break tags if you want the Math Guard question to have more space above and below it.

3. Now you must define an error message in case the user added up the 2 digits incorrectly. If the email is not sent, then the user is not directed to anywhere after implementing Math Guard – the user (email sender) ends up on a blank page, so you need to make the re-directs yourself ~ in addition to adding Math Guard’s code.

So, first things first, please navigate to /language/ and open english.php. Scroll down to find line 145 (it should be at that line) and change it’s contents to something that reflects the error the user experienced. To be precise, this is what is displayed to the user after the error happened. I changed it to this, you may want to do the same:

DEFINE(’_ERROR_MESSAGE’,'Your email wasn\’t sent! Please provide the correct answer.’);

4. Next, find the following code in /components/contact.php, it’s near the top:

switch( $op ) {
case 'sendmail':
sendmail( $con_id, $option );
break;
}

and swap it out with:

switch( $op ) {
case 'sendmail':
require ("ClassMathGuard.php");
if (MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code'] ) ) { sendmail( $con_id, $option );
} else {
$link = sefRelToAbs( ‘index.php?option=com_contact&task=view&contact_id=’. $contact[0]->id .’&Itemid=’. $Itemid );
mosRedirect( $link, _ERROR_MESSAGE );
}
break; }

Quick breakdown: It calls ClassMathGuard.php and then checks the result of what the user typed as an answer. If the answer is correct, the email is sent (sendmail…). If the answer is incorrect this calls up the contact form again and tells the user at the top of the form that they gave the wrong answer (else… do this). If you study the code a little, you can tell what’s going on. I’m not a php expert, I searched for the redirect code and it’s in contact.php further down for their “thankyou” re-direct hack and I used that, so I didn’t re-invent the wheel. LOL. The error message, as you can see, is inserted from the language file we edited before.

We’re done. =D

I hope you found this tutorial easy to follow. =)

Btw, you can see it in action on my Sims 1 site which is Joomla 1.0.13 powered. I had to remove Joomla as it was supposedly using too many server resources as a download site. :(

A big thanks to Code Gravity for MathGuard. It’s great!

What? You want to know how to set up a thankyou page? Well, Joomla redirects to a thankyou page just fine without you doing anything but all it does really is to output a “thankyou for your email” at the top of the page and reloads the contact form. If you want to have one styled to your site and not have it reload the contact form, then… O.k… a small tut will be up soon. =)

Tags:
© 2007

Leave a Reply

Allowed XHTML tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>